Skip to content

Commit 6765ee0

Browse files
Fix #13886 nullptr dereference in warningIntToPointerCast() (danmar#7550)
Co-authored-by: chrchr-github <[email protected]>
1 parent 520147a commit 6765ee0

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

lib/checkother.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ void CheckOther::warningIntToPointerCast()
461461
continue;
462462
if (!tok->valueType() || tok->valueType()->pointer == 0)
463463
continue;
464-
if (!MathLib::isIntHex(from->str()) && from->getKnownIntValue() != 0) {
464+
if (!MathLib::isIntHex(from->str()) && from->hasKnownIntValue() && from->getKnownIntValue() != 0) {
465465
std::string format;
466466
if (MathLib::isDec(from->str()))
467467
format = "decimal";

test/testother.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2211,6 +2211,10 @@ class TestOther : public TestFixture {
22112211

22122212
checkIntToPointerCast("uint8_t* ptr = (uint8_t*)0x7000;"); // <- it's common in embedded code to cast address
22132213
ASSERT_EQUALS("", errout_str());
2214+
2215+
checkIntToPointerCast("struct S { int i; };\n" // #13886, don't crash
2216+
"int f() { return sizeof(((struct S*)0)->i); }");
2217+
ASSERT_EQUALS("", errout_str());
22142218
}
22152219

22162220
#define checkInvalidPointerCast(...) checkInvalidPointerCast_(__FILE__, __LINE__, __VA_ARGS__)

0 commit comments

Comments
 (0)