Skip to content

Commit 905615e

Browse files
committed
Fixed danmar#3878 (Sign extension with unsigned char false positive)
1 parent b9e35b8 commit 905615e

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

lib/checkother.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1794,7 +1794,7 @@ void CheckOther::checkCharVariable()
17941794

17951795
else if (Token::Match(tok, "[;{}] %var% = %any% [&^|] ( * %var% ) ;")) {
17961796
const Variable* var = symbolDatabase->getVariableFromVarId(tok->tokAt(7)->varId());
1797-
if (!var || !var->isPointer() || var->typeStartToken()->str() != "char")
1797+
if (!var || !var->isPointer() || var->typeStartToken()->str() != "char" || var->typeStartToken()->isUnsigned())
17981798
continue;
17991799
// it's ok with a bitwise and where the other operand is 0xff or less..
18001800
if (tok->strAt(4) == "&" && tok->tokAt(3)->isNumber() && MathLib::isGreater("0x100", tok->strAt(3)))

test/testcharvar.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,14 @@ class TestCharVar : public TestFixture {
212212
" return ret;\n"
213213
"}");
214214
ASSERT_EQUALS("", errout.str());
215+
216+
// #3878 - false positive
217+
check("int f(unsigned char *p) {\n"
218+
" int ret = a();\n"
219+
" ret |= *p;\n"
220+
" return ret;\n"
221+
"}");
222+
ASSERT_EQUALS("", errout.str());
215223
}
216224
};
217225

0 commit comments

Comments
 (0)