From 980ca39fe21acc5576835cefe1d376a55dc98192 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 29 Apr 2017 08:25:55 +0200 Subject: [PATCH] valueFlowForward: better handling of sizeof() in rhs in assignments --- lib/valueflow.cpp | 12 ++++++++---- test/testvalueflow.cpp | 7 +++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 549a5b059..ab96a34b1 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -1005,8 +1005,8 @@ static void valueFlowReverse(TokenList *tokenlist, break; } - // skip sizeof.. - if (tok2->str() == ")" && Token::Match(tok2->link()->previous(), "typeof|sizeof (")) + // skip sizeof etc.. + if (tok2->str() == ")" && Token::Match(tok2->link()->previous(), "sizeof|typeof|typeid (")) tok2 = tok2->link(); // goto label @@ -1307,7 +1307,8 @@ static bool valueFlowForward(Token * const startToken, return false; } - if (Token::Match(tok2, "sizeof|typeof|typeid (")) + // Skip sizeof etc + else if (Token::Match(tok2, "sizeof|typeof|typeid (")) tok2 = tok2->linkAt(1); else if (Token::simpleMatch(tok2, "else {")) { @@ -1635,7 +1636,7 @@ static bool valueFlowForward(Token * const startToken, else if (tok2->varId() == varid) { // bailout: assignment - if (Token::Match(tok2->previous(), "!!* %name% %op%") && tok2->next()->isAssignmentOp()) { + if (Token::Match(tok2->previous(), "!!* %name% %assign%")) { // simplify rhs for (Token *tok3 = tok2->tokAt(2); tok3; tok3 = tok3->next()) { if (tok3->varId() == varid) { @@ -1644,6 +1645,9 @@ static bool valueFlowForward(Token * const startToken, setTokenValue(tok3, *it, settings); } else if (Token::Match(tok3, "++|--|?|:|;")) break; + // Skip sizeof etc + else if (Token::Match(tok3, "sizeof|typeof|typeid (")) + tok3 = tok3->linkAt(1); } if (settings->debugwarnings) bailout(tokenlist, errorLogger, tok2, "assignment of " + tok2->str()); diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 00d111f94..ec823f36d 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -2422,6 +2422,13 @@ private: "}"; values = tokenValues(code, "c ."); ASSERT_EQUALS(true, values.size()==1U && values.front().isUninitValue()); + + code = "void f() {\n" + " int **x;\n" + " y += 10;\n" + " x = dostuff(sizeof(*x)*y);\n" + "}"; + ASSERT_EQUALS(0U, tokenValues(code, "x )").size()); } };