From f1074ea1ab27e230102b08cee2268c807e3719b6 Mon Sep 17 00:00:00 2001 From: rikardfalkeborn Date: Tue, 16 Oct 2018 06:54:25 +0200 Subject: [PATCH] Fix false positive: Invalid string argument with pointer to pointer (#1427) If the address is taken inside an array, the address is not of a single character, so do not warn about this. --- lib/checkfunctions.cpp | 2 +- test/testfunctions.cpp | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/checkfunctions.cpp b/lib/checkfunctions.cpp index 380b4ce7a..7634492fe 100644 --- a/lib/checkfunctions.cpp +++ b/lib/checkfunctions.cpp @@ -127,7 +127,7 @@ void CheckFunctions::invalidFunctionUsage() } if (mSettings->library.isargstrz(functionToken, argnr)) { - if (Token::Match(argtok, "& %var%") && argtok->next() && argtok->next()->valueType()) { + if (Token::Match(argtok, "& %var% !![") && argtok->next() && argtok->next()->valueType()) { const ValueType * valueType = argtok->next()->valueType(); const Variable * variable = argtok->next()->variable(); if (valueType->type == ValueType::Type::CHAR && !variable->isGlobal() && diff --git a/test/testfunctions.cpp b/test/testfunctions.cpp index 20cc21082..4d0eb3c7f 100644 --- a/test/testfunctions.cpp +++ b/test/testfunctions.cpp @@ -502,6 +502,9 @@ private: check("size_t f() { char x[] = \"Hello world\"; return strlen(&x[0]) }"); ASSERT_EQUALS("", errout.str()); + check("size_t f() { char* x = \"Hello world\"; return strlen(&x[0]) }"); + ASSERT_EQUALS("", errout.str()); + check("struct S {\n" " char x;\n" "};\n" @@ -527,6 +530,13 @@ private: check("const char x = '\\0'; size_t f() { char y = x; return strlen(&y); }"); ASSERT_EQUALS("", errout.str()); + check("size_t f() {\n" + " char * a = \"Hello world\";\n" + " char ** b = &a;\n" + " return strlen(&b[0][0]);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + // #5225 check("int main(void)\n" "{\n"