Fix #10154 False positive: objectIndex (#3666)

This commit is contained in:
chrchr-github 2022-01-04 10:37:16 +01:00 committed by GitHub
parent 119ec0582a
commit c05e2cc6c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 0 deletions

View File

@ -1015,6 +1015,15 @@ void CheckBufferOverrun::objectIndex()
if (var->valueType()->pointer > obj->valueType()->pointer)
continue;
}
if (obj->valueType() && var->valueType() && (obj->isCast() || (mTokenizer->isCPP() && isCPPCast(obj)) || obj->valueType()->pointer)) { // allow cast to a different type
const auto varSize = var->valueType()->typeSize(*mSettings);
if (varSize == 0)
continue;
if (obj->valueType()->type != var->valueType()->type) {
if (ValueFlow::isOutOfBounds(makeSizeValue(varSize, v.path), idx).empty())
continue;
}
}
if (v.path != 0) {
std::vector<ValueFlow::Value> idxValues;
std::copy_if(idx->values().begin(),

View File

@ -4905,6 +4905,44 @@ private:
" f(&u, N);\n"
"}");
ASSERT_EQUALS("", errout.str());
check("uint32_t f(uint32_t u) {\n" // #10154
" return ((uint8_t*)&u)[3];\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("uint32_t f(uint32_t u) {\n"
" return ((uint8_t*)&u)[4];\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (error) The address of local variable 'u' is accessed at non-zero index.\n", errout.str());
check("uint32_t f(uint32_t u) {\n"
" return reinterpret_cast<unsigned char*>(&u)[3];\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("uint32_t f(uint32_t u) {\n"
" return reinterpret_cast<unsigned char*>(&u)[4];\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:2]: (error) The address of local variable 'u' is accessed at non-zero index.\n", errout.str());
check("uint32_t f(uint32_t u) {\n"
" uint8_t* p = (uint8_t*)&u;\n"
" return p[3];\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("uint32_t f(uint32_t u) {\n"
" uint8_t* p = (uint8_t*)&u;\n"
" return p[4];\n"
"}\n");
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (error) The address of local variable 'u' is accessed at non-zero index.\n", errout.str());
check("uint32_t f(uint32_t* pu) {\n"
" uint8_t* p = (uint8_t*)pu;\n"
" return p[4];\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
};