Skip to content

Commit

Permalink
Fix stack overflow crash in isProviderFor
Browse files Browse the repository at this point in the history
  • Loading branch information
rbuckton committed Dec 14, 2023
1 parent 68fd019 commit 1e66739
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 deletions.
13 changes: 7 additions & 6 deletions Reflect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1745,14 +1745,15 @@ namespace Reflect {
}

function CreateFallbackProvider(reflect: typeof Reflect): MetadataProvider {
const { defineMetadata, hasOwnMetadata, getOwnMetadata, getOwnMetadataKeys, deleteMetadata } = reflect;
const metadataOwner = new _WeakMap<object, Set<string | symbol | undefined>>();
const provider: MetadataProvider = {
isProviderFor(O, P) {
let metadataPropertySet = metadataOwner.get(O);
if (!IsUndefined(metadataPropertySet)) {
return metadataPropertySet.has(P);
}
if (reflect.getOwnMetadataKeys(O, P!).length) {
if (getOwnMetadataKeys(O, P!).length) {
if (IsUndefined(metadataPropertySet)) {
metadataPropertySet = new _Set();
metadataOwner.set(O, metadataPropertySet);
Expand All @@ -1762,11 +1763,11 @@ namespace Reflect {
}
return false;
},
OrdinaryDefineOwnMetadata: reflect.defineMetadata,
OrdinaryHasOwnMetadata: reflect.hasOwnMetadata,
OrdinaryGetOwnMetadata: reflect.getOwnMetadata,
OrdinaryOwnMetadataKeys: reflect.getOwnMetadataKeys,
OrdinaryDeleteMetadata: reflect.deleteMetadata,
OrdinaryDefineOwnMetadata: defineMetadata,
OrdinaryHasOwnMetadata: hasOwnMetadata,
OrdinaryGetOwnMetadata: getOwnMetadata,
OrdinaryOwnMetadataKeys: getOwnMetadataKeys,
OrdinaryDeleteMetadata: deleteMetadata,
};
return provider;
}
Expand Down
13 changes: 7 additions & 6 deletions ReflectLite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1705,14 +1705,15 @@ namespace Reflect {
}

function CreateFallbackProvider(reflect: typeof Reflect): MetadataProvider {
const { defineMetadata, hasOwnMetadata, getOwnMetadata, getOwnMetadataKeys, deleteMetadata } = reflect;
const metadataOwner = new _WeakMap<object, Set<string | symbol | undefined>>();
const provider: MetadataProvider = {
isProviderFor(O, P) {
let metadataPropertySet = metadataOwner.get(O);
if (!IsUndefined(metadataPropertySet)) {
return metadataPropertySet.has(P);
}
if (reflect.getOwnMetadataKeys(O, P!).length) {
if (getOwnMetadataKeys(O, P!).length) {
if (IsUndefined(metadataPropertySet)) {
metadataPropertySet = new _Set();
metadataOwner.set(O, metadataPropertySet);
Expand All @@ -1722,11 +1723,11 @@ namespace Reflect {
}
return false;
},
OrdinaryDefineOwnMetadata: reflect.defineMetadata,
OrdinaryHasOwnMetadata: reflect.hasOwnMetadata,
OrdinaryGetOwnMetadata: reflect.getOwnMetadata,
OrdinaryOwnMetadataKeys: reflect.getOwnMetadataKeys,
OrdinaryDeleteMetadata: reflect.deleteMetadata,
OrdinaryDefineOwnMetadata: defineMetadata,
OrdinaryHasOwnMetadata: hasOwnMetadata,
OrdinaryGetOwnMetadata: getOwnMetadata,
OrdinaryOwnMetadataKeys: getOwnMetadataKeys,
OrdinaryDeleteMetadata: deleteMetadata,
};
return provider;
}
Expand Down
13 changes: 7 additions & 6 deletions ReflectNoConflict.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1582,14 +1582,15 @@ function CreateMetadataProvider(registry: MetadataRegistry): MetadataProvider {
}

function CreateFallbackProvider(reflect: typeof Reflect): MetadataProvider {
const { defineMetadata, hasOwnMetadata, getOwnMetadata, getOwnMetadataKeys, deleteMetadata } = reflect;
const metadataOwner = new _WeakMap<object, Set<string | symbol | undefined>>();
const provider: MetadataProvider = {
isProviderFor(O, P) {
let metadataPropertySet = metadataOwner.get(O);
if (!IsUndefined(metadataPropertySet)) {
return metadataPropertySet.has(P);
}
if (reflect.getOwnMetadataKeys(O, P!).length) {
if (getOwnMetadataKeys(O, P!).length) {
if (IsUndefined(metadataPropertySet)) {
metadataPropertySet = new _Set();
metadataOwner.set(O, metadataPropertySet);
Expand All @@ -1599,11 +1600,11 @@ function CreateFallbackProvider(reflect: typeof Reflect): MetadataProvider {
}
return false;
},
OrdinaryDefineOwnMetadata: reflect.defineMetadata,
OrdinaryHasOwnMetadata: reflect.hasOwnMetadata,
OrdinaryGetOwnMetadata: reflect.getOwnMetadata,
OrdinaryOwnMetadataKeys: reflect.getOwnMetadataKeys,
OrdinaryDeleteMetadata: reflect.deleteMetadata,
OrdinaryDefineOwnMetadata: defineMetadata,
OrdinaryHasOwnMetadata: hasOwnMetadata,
OrdinaryGetOwnMetadata: getOwnMetadata,
OrdinaryOwnMetadataKeys: getOwnMetadataKeys,
OrdinaryDeleteMetadata: deleteMetadata,
};
return provider;
}
Expand Down
12 changes: 12 additions & 0 deletions test/reflect-other.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,17 @@ for (const { name, header, context } of suites.filter(s => s.global)) {
assert.strictEqual(Reflect.getOwnMetadata("key", obj), "value");
});
});

it("isProviderFor crash", () => {
const { Reflect } = script(context)`
Reflect.defineMetadata = function() {};
Reflect.getOwnMetadataKeys = function() { return [] };
Reflect.getMetadataKeys = function() { return []; }
${header}
exports.Reflect = Reflect;
`;
let obj = {};
Reflect.getMetadataKeys(obj);
});
});
}

0 comments on commit 1e66739

Please sign in to comment.