From 08f657f4a42563b32ab370c4afd75b6ff736a236 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Sat, 19 Dec 2020 18:38:08 +0100 Subject: [PATCH] glr2.cc: fix calling conventions for yyexpandGLRStackIfNeeded This test fails: 748: Incorrect lookahead during nondeterministic GLR: glr2.cc It consumes lots of stack space, so at some point we need to expand it. Because of Boolean logic mistakes, we then claim memory-exhausted (first error). Hence we jump to cleaning the stack (popall_), calling all the destructors, and at some point we crash with heap-use-after-free (second error). This commit fixes the first error. Unfortunately, even though we now do expand the stack, we crash again with (another) heap-use-after-free, not addressed here. Eventually, we should make sure popall_() properly works. * data/skeletons/glr2.cc (yyexpandGLRStackIfNeeded): Return true iff success (i.e., memory not exhausted). --- data/skeletons/glr2.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/data/skeletons/glr2.cc b/data/skeletons/glr2.cc index 28241981..209c976f 100644 --- a/data/skeletons/glr2.cc +++ b/data/skeletons/glr2.cc @@ -1449,7 +1449,7 @@ class state_stack { /** Returns false if it tried to expand but could not. */ bool yyexpandGLRStackIfNeeded() { - return spaceLeft() < YYHEADROOM && yyexpandGLRStack(); + return YYHEADROOM <= spaceLeft() || yyexpandGLRStack(); } private: @@ -1471,7 +1471,7 @@ class state_stack { #else bool yyexpandGLRStackIfNeeded () { - return spaceLeft () < YYHEADROOM; + return YYHEADROOM <= spaceLeft (); } #endif @@ -1895,7 +1895,7 @@ public: ]b4_namespace_ref[::]b4_parser_class[& yyparser; void yyreserveGlrStack() { - if (yystateStack.yyexpandGLRStackIfNeeded ()) + if (!yystateStack.yyexpandGLRStackIfNeeded ()) yyMemoryExhausted(); }