From 1c752572127733353139bc4088b42c8d745dea8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 27 Jul 2019 08:25:07 +0200 Subject: [PATCH] Token::hasKnownIntValue: Fix when there is one possible int value and one known BUFFER_SIZE value, it should return false. --- lib/token.h | 8 +++++++- test/testtoken.cpp | 17 +++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/token.h b/lib/token.h index 45ce885fa..e8e75ed74 100644 --- a/lib/token.h +++ b/lib/token.h @@ -936,7 +936,13 @@ public: } bool hasKnownIntValue() const { - return hasKnownValue() && std::any_of(mImpl->mValues->begin(), mImpl->mValues->end(), std::mem_fn(&ValueFlow::Value::isIntValue)); + if (!mImpl->mValues) + return false; + for (const ValueFlow::Value &value : *mImpl->mValues) { + if (value.isKnown() && value.isIntValue()) + return true; + } + return false; } bool hasKnownValue() const { diff --git a/test/testtoken.cpp b/test/testtoken.cpp index 36387ffb9..99b8dfb54 100644 --- a/test/testtoken.cpp +++ b/test/testtoken.cpp @@ -106,6 +106,8 @@ private: TEST_CASE(findClosingBracket); TEST_CASE(expressionString); + + TEST_CASE(hasKnownIntValue); } void nextprevious() const { @@ -989,6 +991,21 @@ private: givenACodeSampleToTokenize data4("return L\"a\";"); ASSERT_EQUALS("returnL\"a\"", data4.tokens()->expressionString()); } + + void hasKnownIntValue() { + // pointer might be NULL + ValueFlow::Value v1(0); + + // pointer points at buffer that is 2 bytes + ValueFlow::Value v2(2); + v2.valueType = ValueFlow::Value::BUFFER_SIZE; + v2.setKnown(); + + Token token; + ASSERT_EQUALS(true, token.addValue(v1)); + ASSERT_EQUALS(true, token.addValue(v2)); + ASSERT_EQUALS(false, token.hasKnownIntValue()); + } }; REGISTER_TEST(TestToken)