Skip to content

Commit 214f90c

Browse files
committed
Bug hunting; Fix false positive, unsigned array index can't be negative
1 parent 1d6c097 commit 214f90c

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

lib/bughuntingchecks.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ static void arrayIndex(const Token *tok, const ExprEngine::Value &value, ExprEng
7272
bailout);
7373
}
7474
}
75-
if (value.isLessThan(dataBase, 0)) {
75+
bool isUnsigned = tok->valueType() && tok->valueType()->sign == ::ValueType::Sign::UNSIGNED;
76+
if (!isUnsigned && value.isLessThan(dataBase, 0)) {
7677
const bool bailout = (value.type == ExprEngine::ValueType::BailoutValue);
7778
dataBase->reportError(tok,
7879
Severity::SeverityType::error,

test/testbughuntingchecks.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class TestBughuntingChecks : public TestFixture {
4141
TEST_CASE(arrayIndexOutOfBounds3);
4242
TEST_CASE(arrayIndexOutOfBounds4);
4343
TEST_CASE(arrayIndexOutOfBounds5);
44+
TEST_CASE(arrayIndexOutOfBounds6);
4445
TEST_CASE(arrayIndexOutOfBoundsDim1);
4546
TEST_CASE(bufferOverflowMemCmp1);
4647
TEST_CASE(bufferOverflowMemCmp2);
@@ -157,6 +158,14 @@ class TestBughuntingChecks : public TestFixture {
157158
"[test.cpp:9]: (error) Cannot determine that 'buf[i]' is initialized\n",
158159
errout.str());
159160
}
161+
void arrayIndexOutOfBounds6() {
162+
check("int buf[5];\n"
163+
"uint16_t foo(size_t offset) {\n"
164+
" uint8_t c = (offset & 0xc0) >> 6;\n"
165+
" return 2 * buf[c];\n"
166+
"}");
167+
ASSERT_EQUALS("", errout.str());
168+
}
160169

161170
void arrayIndexOutOfBoundsDim1() { // itc test case
162171
check("void overrun_st_008 () {\n"

0 commit comments

Comments
 (0)