Skip to content

Commit 7039fac

Browse files
sapbenbenbenben.sap
andauthored
check potential stackoverflow before parsing BinaryExpression (#38)
* handle stackoverflow * handle stackoverflow --------- Co-authored-by: benben.sap <benben.sap@antgroup.com>
1 parent 5f73f2a commit 7039fac

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

parser-Java-Js/src/frontend/java/ASTBuilder.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,10 +1385,14 @@ export class ASTBuilder
13851385
// expression
13861386
else if (['*', '/', '%', '+', '-', '<=', '>=', '>', '<', '>>', '<<', '==', '!=', '&', '|', '^', '&&', '||'].indexOf(bopText) !== -1) {
13871387
// e.g. a*b
1388-
const left = this.visit(ctx.expression()[0]) as UAST.Expression;
1389-
const right = this.visit(ctx.expression()[1]) as UAST.Expression;
1390-
// @ts-ignore
1391-
return UAST.binaryExpression(bopText, left, right);
1388+
if (bopText === '+' && willExceedStackLimit(ctx.text)) {
1389+
return UAST.noop()
1390+
} else {
1391+
const left = this.visit(ctx.expression()[0]) as UAST.Expression;
1392+
const right = this.visit(ctx.expression()[1]) as UAST.Expression;
1393+
// @ts-ignore
1394+
return UAST.binaryExpression(bopText, left, right);
1395+
}
13921396
} else if (['=', '^=', '&=', '<<=', '>>=', '>>>=', '+=', '-=', '*=', '/=', '%=', ',=', '**=', '|='].indexOf(bopText) !== -1) {
13931397
// e.g. a=b
13941398
const left = this.visit(ctx.expression()[0]) as UAST.Expression;
@@ -2503,6 +2507,12 @@ function addLoc(target, name, descriptor) {
25032507
return descriptor;
25042508
}
25052509

2510+
function willExceedStackLimit(str) {
2511+
if (!str) {
2512+
return false
2513+
}
2514+
return str.split('+').length > 1000
2515+
}
25062516

25072517
function getUpperCase(str) {
25082518
return str.charAt(0).toUpperCase() + str.slice(1);

0 commit comments

Comments
 (0)