History

Guard binary expressions from too much recursion.

Tor Andersson committed 8 years ago

Commit 4d45a961 parent 8df3dcd1 file · +58 −18Browse files

Unified · Split

jsparse.c+58−18
@@ -25,6 +25,8 @@ JS_NORETURN static void jsP_error(js_State *J, const char *fmt, ...) JS_PRINTFLI · 24 unmodified lines
2525
2626 #define INCREC() if (++J->astdepth > JS_ASTLIMIT) jsP_error(J, "too much recursion")
2727 #define DECREC() --J->astdepth
28+#define SAVEREC() int SAVE=J->astdepth
29+#define POPREC() J->astdepth=SAVE
2830
2931 static void jsP_error(js_State *J, const char *fmt, ...)
3032 {
@@ -383,23 +385,26 @@ static js_Ast *newexp(js_State *J) · 352 unmodified lines
383385
384386 static js_Ast *memberexp(js_State *J)
385387 {
386− js_Ast *a;
387− INCREC();
388− a = newexp(J);
388+ js_Ast *a = newexp(J);
389+ SAVEREC();
389390 loop:
391+ INCREC();
390392 if (jsP_accept(J, '.')) { a = EXP2(MEMBER, a, identifiername(J)); goto loop; }
391393 if (jsP_accept(J, '[')) { a = EXP2(INDEX, a, expression(J, 0)); jsP_expect(J, ']'); goto loop; }
392− DECREC();
394+ POPREC();
393395 return a;
394396 }
395397
396398 static js_Ast *callexp(js_State *J)
397399 {
398400 js_Ast *a = newexp(J);
401+ SAVEREC();
399402 loop:
403+ INCREC();
400404 if (jsP_accept(J, '.')) { a = EXP2(MEMBER, a, identifiername(J)); goto loop; }
401405 if (jsP_accept(J, '[')) { a = EXP2(INDEX, a, expression(J, 0)); jsP_expect(J, ']'); goto loop; }
402406 if (jsP_accept(J, '(')) { a = EXP2(CALL, a, arguments(J)); jsP_expect(J, ')'); goto loop; }
407+ POPREC();
403408 return a;
404409 }
405410
@@ -432,104 +437,139 @@ static js_Ast *unary(js_State *J) · 26 unmodified lines
432437 static js_Ast *multiplicative(js_State *J)
433438 {
434439 js_Ast *a = unary(J);
440+ SAVEREC();
435441 loop:
442+ INCREC();
436443 if (jsP_accept(J, '*')) { a = EXP2(MUL, a, unary(J)); goto loop; }
437444 if (jsP_accept(J, '/')) { a = EXP2(DIV, a, unary(J)); goto loop; }
438445 if (jsP_accept(J, '%')) { a = EXP2(MOD, a, unary(J)); goto loop; }
446+ POPREC();
439447 return a;
440448 }
441449
442450 static js_Ast *additive(js_State *J)
443451 {
444452 js_Ast *a = multiplicative(J);
453+ SAVEREC();
445454 loop:
455+ INCREC();
446456 if (jsP_accept(J, '+')) { a = EXP2(ADD, a, multiplicative(J)); goto loop; }
447457 if (jsP_accept(J, '-')) { a = EXP2(SUB, a, multiplicative(J)); goto loop; }
458+ POPREC();
448459 return a;
449460 }
450461
451462 static js_Ast *shift(js_State *J)
452463 {
453464 js_Ast *a = additive(J);
465+ SAVEREC();
454466 loop:
467+ INCREC();
455468 if (jsP_accept(J, TK_SHL)) { a = EXP2(SHL, a, additive(J)); goto loop; }
456469 if (jsP_accept(J, TK_SHR)) { a = EXP2(SHR, a, additive(J)); goto loop; }
457470 if (jsP_accept(J, TK_USHR)) { a = EXP2(USHR, a, additive(J)); goto loop; }
471+ POPREC();
458472 return a;
459473 }
460474
461475 static js_Ast *relational(js_State *J, int notin)
462476 {
463477 js_Ast *a = shift(J);
478+ SAVEREC();
464479 loop:
480+ INCREC();
465481 if (jsP_accept(J, '<')) { a = EXP2(LT, a, shift(J)); goto loop; }
466482 if (jsP_accept(J, '>')) { a = EXP2(GT, a, shift(J)); goto loop; }
467483 if (jsP_accept(J, TK_LE)) { a = EXP2(LE, a, shift(J)); goto loop; }
468484 if (jsP_accept(J, TK_GE)) { a = EXP2(GE, a, shift(J)); goto loop; }
469485 if (jsP_accept(J, TK_INSTANCEOF)) { a = EXP2(INSTANCEOF, a, shift(J)); goto loop; }
470486 if (!notin && jsP_accept(J, TK_IN)) { a = EXP2(IN, a, shift(J)); goto loop; }
487+ POPREC();
471488 return a;
472489 }
473490
474491 static js_Ast *equality(js_State *J, int notin)
475492 {
476493 js_Ast *a = relational(J, notin);
494+ SAVEREC();
477495 loop:
496+ INCREC();
478497 if (jsP_accept(J, TK_EQ)) { a = EXP2(EQ, a, relational(J, notin)); goto loop; }
479498 if (jsP_accept(J, TK_NE)) { a = EXP2(NE, a, relational(J, notin)); goto loop; }
480499 if (jsP_accept(J, TK_STRICTEQ)) { a = EXP2(STRICTEQ, a, relational(J, notin)); goto loop; }
481500 if (jsP_accept(J, TK_STRICTNE)) { a = EXP2(STRICTNE, a, relational(J, notin)); goto loop; }
501+ POPREC();
482502 return a;
483503 }
484504
485505 static js_Ast *bitand(js_State *J, int notin)
486506 {
487507 js_Ast *a = equality(J, notin);
488− while (jsP_accept(J, '&'))
508+ SAVEREC();
509+ while (jsP_accept(J, '&')) {
510+ INCREC();
489511 a = EXP2(BITAND, a, equality(J, notin));
512+ }
513+ POPREC();
490514 return a;
491515 }
492516
493517 static js_Ast *bitxor(js_State *J, int notin)
494518 {
495519 js_Ast *a = bitand(J, notin);
496− while (jsP_accept(J, '^'))
520+ SAVEREC();
521+ while (jsP_accept(J, '^')) {
522+ INCREC();
497523 a = EXP2(BITXOR, a, bitand(J, notin));
524+ }
525+ POPREC();
498526 return a;
499527 }
500528
501529 static js_Ast *bitor(js_State *J, int notin)
502530 {
503531 js_Ast *a = bitxor(J, notin);
504− while (jsP_accept(J, '|'))
532+ SAVEREC();
533+ while (jsP_accept(J, '|')) {
534+ INCREC();
505535 a = EXP2(BITOR, a, bitxor(J, notin));
536+ }
537+ POPREC();
506538 return a;
507539 }
508540
509541 static js_Ast *logand(js_State *J, int notin)
510542 {
511543 js_Ast *a = bitor(J, notin);
512− if (jsP_accept(J, TK_AND))
544+ if (jsP_accept(J, TK_AND)) {
545+ INCREC();
513546 a = EXP2(LOGAND, a, logand(J, notin));
547+ DECREC();
548+ }
514549 return a;
515550 }
516551
517552 static js_Ast *logor(js_State *J, int notin)
518553 {
519554 js_Ast *a = logand(J, notin);
520− if (jsP_accept(J, TK_OR))
555+ if (jsP_accept(J, TK_OR)) {
556+ INCREC();
521557 a = EXP2(LOGOR, a, logor(J, notin));
558+ DECREC();
559+ }
522560 return a;
523561 }
524562
525563 static js_Ast *conditional(js_State *J, int notin)
526564 {
527− js_Ast *a, *b, *c;
528− a = logor(J, notin);
565+ js_Ast *a = logor(J, notin);
529566 if (jsP_accept(J, '?')) {
567+ js_Ast *b, *c;
568+ INCREC();
530569 b = assignment(J, 0);
531570 jsP_expect(J, ':');
532571 c = assignment(J, notin);
572+ DECREC();
533573 return EXP3(COND, a, b, c);
534574 }
535575 return a;
@@ -537,9 +577,8 @@ static js_Ast *conditional(js_State *J, int notin) · 1 unmodified line
537577
538578 static js_Ast *assignment(js_State *J, int notin)
539579 {
540− js_Ast *a;
580+ js_Ast *a = conditional(J, notin);
541581 INCREC();
542− a = conditional(J, notin);
543582 if (jsP_accept(J, '=')) a = EXP2(ASS, a, assignment(J, notin));
544583 else if (jsP_accept(J, TK_MUL_ASS)) a = EXP2(ASS_MUL, a, assignment(J, notin));
545584 else if (jsP_accept(J, TK_DIV_ASS)) a = EXP2(ASS_DIV, a, assignment(J, notin));
@@ -558,12 +597,13 @@ static js_Ast *assignment(js_State *J, int notin) · 12 unmodified lines
558597
559598 static js_Ast *expression(js_State *J, int notin)
560599 {
561− js_Ast *a;
562− INCREC();
563− a = assignment(J, notin);
564− while (jsP_accept(J, ','))
600+ js_Ast *a = assignment(J, notin);
601+ SAVEREC();
602+ while (jsP_accept(J, ',')) {
603+ INCREC();
565604 a = EXP2(COMMA, a, assignment(J, notin));
566− DECREC();
605+ }
606+ POPREC();
567607 return a;
568608 }
569609

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t