From b47ce080e0276a6e9a2cada1e21a8f2aa9957cc3 Mon Sep 17 00:00:00 2001 From: Paul Date: Sun, 10 Apr 2022 18:52:05 -0500 Subject: [PATCH 1/2] ValueFlow: Set values to parameters to escape functions --- 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 7b9b98bd9cd..5c726daa49c 100644 --- a/lib/forwardanalyzer.cpp +++ b/lib/forwardanalyzer.cpp @@ -131,9 +131,13 @@ struct ForwardTraversal { // If we are in a loop then jump to the end if (out) *out = loopEnds.back(); - } else if (Token::Match(tok, "return|throw") || isEscapeFunction(tok, &settings->library)) { - traverseRecursive(tok->astOperand1(), f, traverseUnknown); + } else if (Token::Match(tok, "return|throw")) { traverseRecursive(tok->astOperand2(), f, traverseUnknown); + traverseRecursive(tok->astOperand1(), f, traverseUnknown); + return Break(Analyzer::Terminate::Escape); + } else if (Token::Match(tok, "%name% (") && isEscapeFunction(tok, &settings->library)) { + // Traverse the parameters of the function before escaping + traverseRecursive(tok->next()->astOperand2(), f, traverseUnknown); return Break(Analyzer::Terminate::Escape); } else if (isUnevaluated(tok)) { if (out) diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 70aa168adee..93a28e82134 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -3472,6 +3472,12 @@ class TestValueFlow : public TestFixture { " if (x == 42) {}\n" "}"; ASSERT_EQUALS(false, testValueOfX(code, 6U, 11)); + + code = "void f() {\n" + " int x = 1;\n" + " exit(x);\n" + "}\n"; + ASSERT_EQUALS(true, testValueOfXKnown(code, 3U, 1)); } void valueFlowForwardTernary() { From 3202ed090c658c35fb95a720e087983c28ad2374 Mon Sep 17 00:00:00 2001 From: Paul Date: Sun, 10 Apr 2022 18:52:45 -0500 Subject: [PATCH 2/2] Format --- test/testvalueflow.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 93a28e82134..c89e9e94ee3 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -3474,9 +3474,9 @@ class TestValueFlow : public TestFixture { ASSERT_EQUALS(false, testValueOfX(code, 6U, 11)); code = "void f() {\n" - " int x = 1;\n" - " exit(x);\n" - "}\n"; + " int x = 1;\n" + " exit(x);\n" + "}\n"; ASSERT_EQUALS(true, testValueOfXKnown(code, 3U, 1)); }