Skip to content

Commit 6909574

Browse files
committed
Limit symbol instantiations to a maximum depth of 100
1 parent 1c64943 commit 6909574

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

src/compiler/checker.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ namespace ts {
4747

4848
let typeCount = 0;
4949
let symbolCount = 0;
50+
let symbolInstantiationDepth = 0;
5051

5152
const emptyArray: any[] = [];
5253
const emptySymbols = createMap<Symbol>();
@@ -4440,14 +4441,22 @@ namespace ts {
44404441
function getTypeOfInstantiatedSymbol(symbol: Symbol): Type {
44414442
const links = getSymbolLinks(symbol);
44424443
if (!links.type) {
4443-
if (!pushTypeResolution(symbol, TypeSystemPropertyName.Type)) {
4444-
return unknownType;
4444+
if (symbolInstantiationDepth === 100) {
4445+
error(symbol.valueDeclaration, Diagnostics.Generic_type_instantiation_is_excessively_deep_and_possibly_infinite);
4446+
links.type = unknownType;
44454447
}
4446-
let type = instantiateType(getTypeOfSymbol(links.target), links.mapper);
4447-
if (!popTypeResolution()) {
4448-
type = reportCircularityError(symbol);
4448+
else {
4449+
if (!pushTypeResolution(symbol, TypeSystemPropertyName.Type)) {
4450+
return unknownType;
4451+
}
4452+
symbolInstantiationDepth++;
4453+
let type = instantiateType(getTypeOfSymbol(links.target), links.mapper);
4454+
symbolInstantiationDepth--;
4455+
if (!popTypeResolution()) {
4456+
type = reportCircularityError(symbol);
4457+
}
4458+
links.type = type;
44494459
}
4450-
links.type = type;
44514460
}
44524461
return links.type;
44534462
}
@@ -7257,8 +7266,9 @@ namespace ts {
72577266
else {
72587267
error(indexNode, Diagnostics.Type_0_cannot_be_used_as_an_index_type, typeToString(indexType));
72597268
}
7269+
return unknownType;
72607270
}
7261-
return unknownType;
7271+
return anyType;
72627272
}
72637273

72647274
function getIndexedAccessForMappedType(type: MappedType, indexType: Type, accessNode?: ElementAccessExpression | IndexedAccessTypeNode) {

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1827,6 +1827,10 @@
18271827
"category": "Error",
18281828
"code": 2549
18291829
},
1830+
"Generic type instantiation is excessively deep and possibly infinite.": {
1831+
"category": "Error",
1832+
"code": 2550
1833+
},
18301834
"JSX element attributes type '{0}' may not be a union type.": {
18311835
"category": "Error",
18321836
"code": 2600

0 commit comments

Comments
 (0)