From 0e7be40367245a5fe75a4d8c273b0105a62d9e99 Mon Sep 17 00:00:00 2001 From: Michael Hansen Date: Thu, 28 Aug 2025 15:33:13 -0700 Subject: [PATCH] Add some extra guards against null dereference and empty std::stack pops Fixes segfault cases of #572 --- ASTree.cpp | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/ASTree.cpp b/ASTree.cpp index 3542921..c603e98 100644 --- a/ASTree.cpp +++ b/ASTree.cpp @@ -1231,8 +1231,12 @@ PycRef BuildFromCode(PycRef code, PycModule* mod) break; } - stack = stack_hist.top(); - stack_hist.pop(); + if (!stack_hist.empty()) { + stack = stack_hist.top(); + stack_hist.pop(); + } else { + fprintf(stderr, "Warning: Stack history is empty, something wrong might have happened\n"); + } PycRef prev = curblock; PycRef nil; @@ -1389,10 +1393,10 @@ PycRef BuildFromCode(PycRef code, PycModule* mod) } while (prev != nil); - curblock = blocks.top(); - - if (curblock->blktype() == ASTBlock::BLK_EXCEPT) { - curblock->setEnd(pos+offs); + if (!blocks.empty()) { + curblock = blocks.top(); + if (curblock->blktype() == ASTBlock::BLK_EXCEPT) + curblock->setEnd(pos+offs); } } break; @@ -1769,7 +1773,8 @@ PycRef BuildFromCode(PycRef code, PycModule* mod) else curblock->append(new ASTPrint(stack.top(), stream)); stack.pop(); - stream->setProcessed(); + if (stream) + stream->setProcessed(); } break; case Pyc::PRINT_NEWLINE: @@ -1797,7 +1802,8 @@ PycRef BuildFromCode(PycRef code, PycModule* mod) else curblock->append(new ASTPrint(nullptr, stream)); stack.pop(); - stream->setProcessed(); + if (stream) + stream->setProcessed(); } break; case Pyc::RAISE_VARARGS_A: