From f1212e66f7955cd278cffc5f54ddcaf586b1712d Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 22 Jun 2022 21:22:04 +0200 Subject: [PATCH] Fix FP constVariable with 2D array. (#4228) Test case #8717 was actually a FP as well. --- lib/checkother.cpp | 7 +++++-- test/testother.cpp | 10 +++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index b31f2cdf3..fc3cc333b 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1565,11 +1565,14 @@ void CheckOther::checkConstPointer() if (Token::simpleMatch(gparent, "return")) continue; else if (Token::Match(gparent, "%assign%") && parent == gparent->astOperand2()) { - bool takingRef = false; + bool takingRef = false, nonConstPtrAssignment = false; const Token *lhs = gparent->astOperand1(); if (lhs && lhs->variable() && lhs->variable()->isReference() && lhs->variable()->nameToken() == lhs) takingRef = true; - if (!takingRef) + if (lhs && lhs->valueType() && lhs->valueType()->pointer && (lhs->valueType()->constness & 1) == 0 && + parent->valueType() && parent->valueType()->pointer) + nonConstPtrAssignment = true; + if (!takingRef && !nonConstPtrAssignment) continue; } else if (Token::simpleMatch(gparent, "[") && gparent->astOperand2() == parent) continue; diff --git a/test/testother.cpp b/test/testother.cpp index 4af565858..6446bdf56 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -3230,6 +3230,14 @@ private: ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'p' can be declared as pointer to const\n" "[test.cpp:5]: (style) Variable 'p' can be declared as pointer to const\n", errout.str()); + + check("void f() {\n" + " char a[1][1];\n" + " char* b[1];\n" + " b[0] = a[0];\n" + " **b = 0;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void switchRedundantAssignmentTest() { @@ -9383,7 +9391,7 @@ private: " int local_argc = 0;\n" " local_argv[local_argc++] = argv[0];\n" "}\n", "test.c"); - ASSERT_EQUALS("[test.c:1]: (style) Parameter 'argv' can be declared as const array\n", errout.str()); + ASSERT_EQUALS("", errout.str()); check("void f() {\n" " int x = 0;\n"