diff --git a/lib/checkautovariables.cpp b/lib/checkautovariables.cpp index 6e31495cf..1f8a18502 100644 --- a/lib/checkautovariables.cpp +++ b/lib/checkautovariables.cpp @@ -275,7 +275,7 @@ void CheckAutoVariables::autoVariables() for (const ValueFlow::Value &v : tok->values()) { if (!(v.isTokValue())) continue; - if (isArrayVar(v.tokvalue) || v.tokvalue->tokType() == Token::eString) { + if (isArrayVar(v.tokvalue) || ((v.tokvalue->tokType() == Token::eString) && !v.isImpossible())) { errorInvalidDeallocation(tok, &v); break; } diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index 246e3fbdf..289b54ba5 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -782,6 +782,16 @@ private: "[test.cpp:6]: (error) Deallocation of an pointer pointing to a string literal (\"a\") results in undefined behaviour.\n" "[test.cpp:9]: (error) Deallocation of an pointer pointing to a string literal (\"abc\") results in undefined behaviour.\n", errout.str()); + + check("void f() {\n" + " char *ptr = malloc(10);\n" + " char *empty_str = \"\";\n" + " if (ptr == NULL)\n" + " ptr = empty_str;\n" + " if (ptr != empty_str)\n" + " free(ptr);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void testinvaliddealloc_C() {