Skip to content

Commit 594b442

Browse files
Fix #11754 Confusing message: Size of pointer 'p' used instead of size of its data. (#8566)
1 parent cf48382 commit 594b442

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

lib/checksizeof.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,8 @@ void CheckSizeof::checkSizeofForPointerSize()
233233
// Now check for the sizeof usage: Does the level of pointer indirection match?
234234
const Token * const tokLink = tokSize->linkAt(1);
235235
if (tokLink && tokLink->strAt(-1) == "*") {
236-
if (variable && variable->valueType() && variable->valueType()->pointer == 1 && variable->valueType()->type != ValueType::VOID)
236+
if (variable && variable->valueType() && variable->valueType()->pointer == 1 && variable->valueType()->type != ValueType::VOID &&
237+
variable->valueType()->isTypeEqual(tokSize->next()->astOperand2()->valueType()))
237238
sizeofForPointerError(variable, variable->str());
238239
else if (variable2 && variable2->valueType() && variable2->valueType()->pointer == 1 && variable2->valueType()->type != ValueType::VOID)
239240
sizeofForPointerError(variable2, variable2->str());

test/testsizeof.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,12 @@ class TestSizeof : public TestFixture {
722722
" return AtomName;\n"
723723
"}");
724724
ASSERT_EQUALS("", errout_str());
725+
726+
check("void* f(size_t n) {\n" // #11754
727+
" char* p = malloc(n * sizeof(void*));\n"
728+
" return p;\n"
729+
"}");
730+
ASSERT_EQUALS("", errout_str());
725731
}
726732

727733
void checkPointerSizeofStruct() {

0 commit comments

Comments
 (0)