Skip to content

Commit 66d145b

Browse files
author
Daniel Marjamäki
committed
Fixed cppcheck-opensource#2892 (false positive: (portability) Assigning an address value to the integer (int/long/etc) type is not portable)
1 parent 4055b0e commit 66d145b

5 files changed

Lines changed: 28 additions & 1 deletion

File tree

lib/check64bit.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void Check64BitPortability::pointerassignment()
6161
if (isaddr(var1) && isint(var2))
6262
assignmentIntegerToAddressError(tok->next());
6363

64-
else if (isint(var1) && isaddr(var2))
64+
else if (isint(var1) && isaddr(var2) && !tok->tokAt(3)->isPointerCompare())
6565
assignmentAddressToIntegerError(tok->next());
6666
}
6767
}

lib/token.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Token::Token(Token **t) :
3636
_isBoolean(false),
3737
_isUnsigned(false),
3838
_isSigned(false),
39+
_isPointerCompare(false),
3940
_isLong(false),
4041
_isUnused(false),
4142
_varId(0),
@@ -112,6 +113,7 @@ void Token::deleteThis()
112113
_isBoolean = _next->_isBoolean;
113114
_isUnsigned = _next->_isUnsigned;
114115
_isSigned = _next->_isSigned;
116+
_isPointerCompare = _next->_isPointerCompare;
115117
_isLong = _next->_isLong;
116118
_isUnused = _next->_isUnused;
117119
_varId = _next->_varId;

lib/token.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,14 @@ class Token
219219
{
220220
_isSigned = sign;
221221
}
222+
bool isPointerCompare() const
223+
{
224+
return _isPointerCompare;
225+
}
226+
void isPointerCompare(bool b)
227+
{
228+
_isPointerCompare = b;
229+
}
222230
bool isLong() const
223231
{
224232
return _isLong;
@@ -447,6 +455,7 @@ class Token
447455
bool _isBoolean;
448456
bool _isUnsigned;
449457
bool _isSigned;
458+
bool _isPointerCompare;
450459
bool _isLong;
451460
bool _isUnused;
452461
unsigned int _varId;

lib/tokenize.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ void Tokenizer::insertTokens(Token *dest, const Token *src, unsigned int n)
260260
dest->isBoolean(src->isBoolean());
261261
dest->isUnsigned(src->isUnsigned());
262262
dest->isSigned(src->isSigned());
263+
dest->isPointerCompare(src->isPointerCompare());
263264
dest->isLong(src->isLong());
264265
dest->isUnused(src->isUnused());
265266
src = src->next();
@@ -284,6 +285,7 @@ Token *Tokenizer::copyTokens(Token *dest, const Token *first, const Token *last)
284285
tok2->isBoolean(tok->isBoolean());
285286
tok2->isUnsigned(tok->isUnsigned());
286287
tok2->isSigned(tok->isSigned());
288+
tok2->isPointerCompare(tok->isPointerCompare());
287289
tok2->isLong(tok->isLong());
288290
tok2->isUnused(tok->isUnused());
289291
tok2->varId(tok->varId());
@@ -6477,17 +6479,21 @@ void Tokenizer::simplifyIfNotNull()
64776479
Token::Match(tok, "0 != %var%"))
64786480
{
64796481
deleteFrom = tok->previous();
6482+
if (tok->tokAt(2))
6483+
tok->tokAt(2)->isPointerCompare(true);
64806484
}
64816485

64826486
else if (Token::Match(tok, "%var% != 0"))
64836487
{
64846488
deleteFrom = tok;
6489+
tok->isPointerCompare(true);
64856490
}
64866491

64876492
else if (Token::Match(tok, "%var% .|:: %var% != 0"))
64886493
{
64896494
tok = tok->tokAt(2);
64906495
deleteFrom = tok;
6496+
tok->isPointerCompare(true);
64916497
}
64926498
}
64936499

test/test64bit.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class Test64BitPortability : public TestFixture
3838
TEST_CASE(novardecl);
3939
TEST_CASE(functionpar);
4040
TEST_CASE(structmember);
41+
TEST_CASE(ptrcompare);
4142
}
4243

4344
void check(const char code[])
@@ -101,6 +102,15 @@ class Test64BitPortability : public TestFixture
101102
"}\n");
102103
TODO_ASSERT_EQUALS("[test.cpp:3]: (portability) Assigning an address value to the integer (int/long/etc) type is not portable\n", "", errout.str());
103104
}
105+
106+
void ptrcompare()
107+
{
108+
// Ticket #2892
109+
check("void foo(int *p) {\n"
110+
" int a = (p != NULL);\n"
111+
"}\n");
112+
ASSERT_EQUALS("", errout.str());
113+
}
104114
};
105115

106116
REGISTER_TEST(Test64BitPortability)

0 commit comments

Comments
 (0)