From bf6bcafc5688d5607f3a9c874539c24f9a6b4236 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 11 Apr 2022 20:41:59 +0200 Subject: [PATCH] Fix FP divideSizeof (#4003) --- lib/checksizeof.cpp | 4 +++- test/testsizeof.cpp | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/checksizeof.cpp b/lib/checksizeof.cpp index ed9e8099f..daa08ee62 100644 --- a/lib/checksizeof.cpp +++ b/lib/checksizeof.cpp @@ -387,7 +387,9 @@ void CheckSizeof::suspiciousSizeofCalculation() divideSizeofError(tok); else if (varTok && varTok->str() == "*") { const Token* arrTok = lPar->astParent()->astOperand1(); - arrTok = arrTok ? arrTok->next() : nullptr; + arrTok = arrTok ? arrTok->astOperand2() : nullptr; + while (Token::simpleMatch(arrTok, ".")) + arrTok = arrTok->astOperand2(); var = arrTok ? arrTok->variable() : nullptr; if (var && var->isPointer() && !var->isArray()) divideSizeofError(tok); diff --git a/test/testsizeof.cpp b/test/testsizeof.cpp index 4f4602439..d52380b4e 100644 --- a/test/testsizeof.cpp +++ b/test/testsizeof.cpp @@ -456,6 +456,16 @@ private: " for (size_t i = 0; i < sizeof(t->s) / sizeof(t->s[0]); i++) {}\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + check("struct S {\n" + " struct T {\n" + " char* c[3];\n" + " } t[1];\n" + "};\n" + "void f(S* s) {\n" + " for (int i = 0; i != sizeof(s->t[0].c) / sizeof(char*); i++) {}\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void checkPointerSizeof() {