Skip to content

Commit c34b77c

Browse files
author
Daniel Marjamäki
committed
Fixed danmar#3073 (False positive: Assigning an integer (int/long/etc) to a pointer is not portable)
1 parent 85d83d8 commit c34b77c

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

lib/check64bit.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void Check64BitPortability::pointerassignment()
5858
const Variable *var1(symbolDatabase->getVariableFromVarId(tok->tokAt(1)->varId()));
5959
const Variable *var2(symbolDatabase->getVariableFromVarId(tok->tokAt(3)->varId()));
6060

61-
if (isaddr(var1) && isint(var2))
61+
if (isaddr(var1) && isint(var2) && tok->strAt(4) != "+")
6262
assignmentIntegerToAddressError(tok->next());
6363

6464
else if (isint(var1) && isaddr(var2) && !tok->tokAt(3)->isPointerCompare())

test/test64bit.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class Test64BitPortability : public TestFixture
3939
TEST_CASE(functionpar);
4040
TEST_CASE(structmember);
4141
TEST_CASE(ptrcompare);
42+
TEST_CASE(ptrarithmetic);
4243
}
4344

4445
void check(const char code[])
@@ -118,6 +119,28 @@ class Test64BitPortability : public TestFixture
118119
"}\n");
119120
ASSERT_EQUALS("", errout.str());
120121
}
122+
123+
void ptrarithmetic()
124+
{
125+
// #3073
126+
check("void foo(int *p) {\n"
127+
" int x = 10;\n"
128+
" int *a = p + x;\n"
129+
"}\n");
130+
ASSERT_EQUALS("", errout.str());
131+
132+
check("void foo(int *p) {\n"
133+
" int x = 10;\n"
134+
" int *a = x + p;\n"
135+
"}\n");
136+
ASSERT_EQUALS("", errout.str());
137+
138+
check("void foo(int *p) {\n"
139+
" int x = 10;\n"
140+
" int *a = x * x;\n"
141+
"}\n");
142+
TODO_ASSERT_EQUALS("error", "", errout.str());
143+
}
121144
};
122145

123146
REGISTER_TEST(Test64BitPortability)

0 commit comments

Comments
 (0)