autovarInvalidDeallocation: Fix fp with impossible value (#3604)

daca reports new false positives after db4f94fdfe on the form:

	void f() {
	    char *ptr = malloc(10);
	    char *empty_str = "";
	    if (ptr == NULL)
	        ptr = empty_str;
	    if (ptr != empty_str)
	        free(ptr);
	}

Add a check that the value is not impossible to avoid this.
This commit is contained in:
Rikard Falkeborn 2021-12-05 15:47:21 +01:00 committed by GitHub
parent f64bcac004
commit 1a50146745
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 1 deletions

View File

@ -275,7 +275,7 @@ void CheckAutoVariables::autoVariables()
for (const ValueFlow::Value &v : tok->values()) { for (const ValueFlow::Value &v : tok->values()) {
if (!(v.isTokValue())) if (!(v.isTokValue()))
continue; continue;
if (isArrayVar(v.tokvalue) || v.tokvalue->tokType() == Token::eString) { if (isArrayVar(v.tokvalue) || ((v.tokvalue->tokType() == Token::eString) && !v.isImpossible())) {
errorInvalidDeallocation(tok, &v); errorInvalidDeallocation(tok, &v);
break; break;
} }

View File

@ -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: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", "[test.cpp:9]: (error) Deallocation of an pointer pointing to a string literal (\"abc\") results in undefined behaviour.\n",
errout.str()); 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() { void testinvaliddealloc_C() {