Skip to content

Commit f1212e6

Browse files
Fix FP constVariable with 2D array. (#4228)
Test case #8717 was actually a FP as well.
1 parent 650f409 commit f1212e6

2 files changed

Lines changed: 14 additions & 3 deletions

File tree

lib/checkother.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,11 +1565,14 @@ void CheckOther::checkConstPointer()
15651565
if (Token::simpleMatch(gparent, "return"))
15661566
continue;
15671567
else if (Token::Match(gparent, "%assign%") && parent == gparent->astOperand2()) {
1568-
bool takingRef = false;
1568+
bool takingRef = false, nonConstPtrAssignment = false;
15691569
const Token *lhs = gparent->astOperand1();
15701570
if (lhs && lhs->variable() && lhs->variable()->isReference() && lhs->variable()->nameToken() == lhs)
15711571
takingRef = true;
1572-
if (!takingRef)
1572+
if (lhs && lhs->valueType() && lhs->valueType()->pointer && (lhs->valueType()->constness & 1) == 0 &&
1573+
parent->valueType() && parent->valueType()->pointer)
1574+
nonConstPtrAssignment = true;
1575+
if (!takingRef && !nonConstPtrAssignment)
15731576
continue;
15741577
} else if (Token::simpleMatch(gparent, "[") && gparent->astOperand2() == parent)
15751578
continue;

test/testother.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3230,6 +3230,14 @@ class TestOther : public TestFixture {
32303230
ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'p' can be declared as pointer to const\n"
32313231
"[test.cpp:5]: (style) Variable 'p' can be declared as pointer to const\n",
32323232
errout.str());
3233+
3234+
check("void f() {\n"
3235+
" char a[1][1];\n"
3236+
" char* b[1];\n"
3237+
" b[0] = a[0];\n"
3238+
" **b = 0;\n"
3239+
"}\n");
3240+
ASSERT_EQUALS("", errout.str());
32333241
}
32343242

32353243
void switchRedundantAssignmentTest() {
@@ -9383,7 +9391,7 @@ class TestOther : public TestFixture {
93839391
" int local_argc = 0;\n"
93849392
" local_argv[local_argc++] = argv[0];\n"
93859393
"}\n", "test.c");
9386-
ASSERT_EQUALS("[test.c:1]: (style) Parameter 'argv' can be declared as const array\n", errout.str());
9394+
ASSERT_EQUALS("", errout.str());
93879395

93889396
check("void f() {\n"
93899397
" int x = 0;\n"

0 commit comments

Comments
 (0)