From 4eed29502cdb962ac715f4a6740197abc5fadf0f Mon Sep 17 00:00:00 2001 From: Paul Fultz II Date: Fri, 22 Apr 2022 23:18:55 -0500 Subject: [PATCH] ValueFlow: Set values to parameters to longjmp and setjmp (#4040) * ValueFlow: Set values to parameters to longjmp and setjmp * Format --- lib/forwardanalyzer.cpp | 8 ++++++-- test/testvalueflow.cpp | 6 ++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/forwardanalyzer.cpp b/lib/forwardanalyzer.cpp index 5c726daa4..07460af58 100644 --- a/lib/forwardanalyzer.cpp +++ b/lib/forwardanalyzer.cpp @@ -123,9 +123,13 @@ struct ForwardTraversal { template )> Progress traverseTok(T* tok, F f, bool traverseUnknown, T** out = nullptr) { - if (Token::Match(tok, "asm|goto|setjmp|longjmp")) + if (Token::Match(tok, "asm|goto")) return Break(Analyzer::Terminate::Bail); - else if (Token::simpleMatch(tok, "continue")) { + else if (Token::Match(tok, "setjmp|longjmp (")) { + // Traverse the parameters of the function before escaping + traverseRecursive(tok->next()->astOperand2(), f, traverseUnknown); + return Break(Analyzer::Terminate::Bail); + } else if (Token::simpleMatch(tok, "continue")) { if (loopEnds.empty()) return Break(Analyzer::Terminate::Escape); // If we are in a loop then jump to the end diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index b6b3f4bed..404c00a5a 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -3486,6 +3486,12 @@ private: " exit(x);\n" "}\n"; ASSERT_EQUALS(true, testValueOfXKnown(code, 3U, 1)); + + code = "void f(jmp_buf env) {\n" + " int x = 1;\n" + " longjmp(env, x);\n" + "}\n"; + ASSERT_EQUALS(true, testValueOfXKnown(code, 3U, 1)); } void valueFlowForwardTernary() {