Remove the CodeQL findings in typegen and PortableRegistry

- typegen formatType: the inline-struct shortcut test /^{.+:.+}/ has two
  unbounded wildcards and backtracks quadratically on input such as
  '{{a:a:a:...' (370 ms at 20k repetitions). isInlineStruct checks the
  same thing with indexOf: an opening brace, a colon at index 2 or later
  and a closing brace at least two characters after it, all on the
  first line. 500k random inputs give the same answer as the regex; a
  test keeps it under 100 ms at 100k repetitions and goes red on the
  regex.
- PortableRegistry: the type definition cache was a plain object
  written as typeDefs[lookupIndex][key]. lookupIndex is always a number,
  but nothing in the shape said so; it is now a Map, and the definition
  is filled through its own reference.
This commit is contained in:
Satoshi Qazi Muhammed authored and SatoshiQaziMuhammed committed 2026-10-08 03:23:10 +04:00
1 parent 55702e7638
commit 3ff2be4b71
3 files changed
+79 -37

No files matched your search

@@ -537,7 +537,7 @@ export class PortableRegistry extends Struct implements ILookup {
#lookups: Record<string, LookupString>;
#names: Record<number, string>;
#params: TypeInfoParams;
#typeDefs: Record<number, TypeDef> = {};
readonly #typeDefs = new Map<number, TypeDef>();
#types: Record<number, PortableType>;
constructor (registry: Registry, value?: Uint8Array, isContract?: boolean) {
@@ -613,43 +613,46 @@ export class PortableRegistry extends Struct implements ILookup {
*/
public getTypeDef (lookupId: SiLookupTypeId | LookupString | number): TypeDef {
const lookupIndex = this.#getLookupId(lookupId);
const cached = this.#typeDefs.get(lookupIndex);
if (!this.#typeDefs[lookupIndex]) {
const lookupName = this.#names[lookupIndex];
const empty = {
info: TypeDefInfo.DoNotConstruct,
lookupIndex,
lookupName,
type: this.registry.createLookupType(lookupIndex)
};
// Set named items since we will get into circular lookups along the way
if (lookupName) {
this.#typeDefs[lookupIndex] = empty;
}
const extracted = this.#extract(this.getSiType(lookupId), lookupIndex);
// For non-named items, we only set this right at the end
if (!lookupName) {
this.#typeDefs[lookupIndex] = empty;
}
Object.keys(extracted).forEach((k): void => {
if (k !== 'lookupName' || extracted[k]) {
// these are safe since we are looking through the keys as set
this.#typeDefs[lookupIndex][k as 'info'] = extracted[k as 'info'];
}
});
// don't set lookupName on lower-level, we want to always direct to the type
if (extracted.info === TypeDefInfo.Plain) {
this.#typeDefs[lookupIndex].lookupNameRoot = this.#typeDefs[lookupIndex].lookupName;
delete this.#typeDefs[lookupIndex].lookupName;
}
if (cached) {
return cached;
}
return this.#typeDefs[lookupIndex];
const lookupName = this.#names[lookupIndex];
const typeDef: TypeDef = {
info: TypeDefInfo.DoNotConstruct,
lookupIndex,
lookupName,
type: this.registry.createLookupType(lookupIndex)
};
// Set named items since we will get into circular lookups along the way
if (lookupName) {
this.#typeDefs.set(lookupIndex, typeDef);
}
const extracted = this.#extract(this.getSiType(lookupId), lookupIndex);
// For non-named items, we only set this right at the end
if (!lookupName) {
this.#typeDefs.set(lookupIndex, typeDef);
}
Object.keys(extracted).forEach((k): void => {
if (k !== 'lookupName' || extracted[k]) {
// these are safe since we are looking through the keys as set
typeDef[k as 'info'] = extracted[k as 'info'];
}
});
// don't set lookupName on lower-level, we want to always direct to the type
if (extracted.info === TypeDefInfo.Plain) {
typeDef.lookupNameRoot = typeDef.lookupName;
delete typeDef.lookupName;
}
return typeDef;
}
/**