Skip to content

Commit

Permalink
Fix #12019 (False positive: null pointer, array zero initialization) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
danmar committed Sep 28, 2023
1 parent c4fe5ac commit ed5532c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -888,10 +888,16 @@ namespace {
if (!after)
throw InternalError(tok, "Failed to simplify typedef. Is the code valid?");

const Token* const tok4 = useAfterVarRange ? insertTokens(after->previous(), mRangeAfterVar)->next() : tok3->next();
Token* const tok4 = useAfterVarRange ? insertTokens(after->previous(), mRangeAfterVar)->next() : tok3->next();

tok->deleteThis();

// Unsplit variable declarations
if (Token::Match(tok4->previous(), "] ; %name% = {") && tok4->isSplittedVarDeclEq()) {
tok4->deleteNext();
tok4->deleteThis();
}

// Set links
std::stack<Token*> brackets;
for (; tok != tok4; tok = tok->next()) {
Expand Down
10 changes: 9 additions & 1 deletion test/testsimplifytypedef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class TestSimplifyTypedef : public TestFixture {
TEST_CASE(carray1);
TEST_CASE(carray2);
TEST_CASE(carray3);
TEST_CASE(carray4);
TEST_CASE(cdonotreplace1);
TEST_CASE(cppfp1);
TEST_CASE(Generic1);
Expand Down Expand Up @@ -472,6 +473,13 @@ class TestSimplifyTypedef : public TestFixture {
ASSERT_EQUALS("int ( * p ) [ 3 ] [ 2 ] [ 1 ] ;", simplifyTypedef(code));
}

void carray4() {
const char* code{};
code = "typedef int arr[12];\n" // #12019
"void foo() { arr temp = {0}; }\n";
ASSERT_EQUALS("void foo ( ) { int temp [ 12 ] = { 0 } ; }", tok(code));
}

void cdonotreplace1() {
const char code[] = "typedef int t;\n"
"int* t;";
Expand Down Expand Up @@ -1869,7 +1877,7 @@ class TestSimplifyTypedef : public TestFixture {
"}";

// The expected tokens..
const char expected2[] = "void f ( ) { char a [ 256 ] ; a = { 0 } ; char b [ 256 ] ; b = { 0 } ; }";
const char expected2[] = "void f ( ) { char a [ 256 ] = { 0 } ; char b [ 256 ] = { 0 } ; }";
ASSERT_EQUALS(expected2, tok(code2, false, cppcheck::Platform::Type::Native, false));
ASSERT_EQUALS("", errout.str());

Expand Down

0 comments on commit ed5532c

Please sign in to comment.