Skip to content

Commit d5d5b98

Browse files
Refs danmar#6049: Fix verbose ctuPointerArith message (danmar#8162)
1 parent fe492d6 commit d5d5b98

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

lib/ctu.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,20 @@ static bool findPath(const std::string &callId,
559559
return false;
560560
}
561561

562+
static std::string getInvalidValueString(CTU::FileInfo::InvalidValueType invalidValue)
563+
{
564+
using InvalidValueType = CTU::FileInfo::InvalidValueType;
565+
switch (invalidValue) {
566+
case InvalidValueType::null:
567+
return "null";
568+
case InvalidValueType::uninit:
569+
return "uninitialized";
570+
case InvalidValueType::bufferOverflow:
571+
return "accessed out of bounds";
572+
}
573+
cppcheck::unreachable();
574+
}
575+
562576
std::list<ErrorMessage::FileLocation> CTU::FileInfo::getErrorPath(InvalidValueType invalidValue,
563577
const CTU::FileInfo::UnsafeUsage &unsafeUsage,
564578
const std::map<std::string, std::list<const CTU::FileInfo::CallBase *>> &callsMap,
@@ -581,7 +595,7 @@ std::list<ErrorMessage::FileLocation> CTU::FileInfo::getErrorPath(InvalidValueTy
581595

582596
std::list<ErrorMessage::FileLocation> locationList;
583597

584-
const std::string value1 = (invalidValue == InvalidValueType::null) ? "null" : "uninitialized";
598+
const std::string value1 = getInvalidValueString(invalidValue);
585599

586600
for (int index = 9; index >= 0; index--) {
587601
if (!path[index])

test/testbufferoverrun.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5515,6 +5515,19 @@ class TestBufferOverrun : public TestFixture {
55155515
" f(s);\n"
55165516
"}\n");
55175517
ASSERT_EQUALS("", errout_str());
5518+
5519+
setMultiline();
5520+
ctu("void g(char* p) {\n"
5521+
" memset(p + 10, 0, 10);\n"
5522+
"}\n"
5523+
"void f() {\n"
5524+
" char a[10] = {};\n"
5525+
" g(a);\n"
5526+
"}");
5527+
ASSERT_EQUALS("[test.cpp:2:12]: error: Pointer arithmetic overflow; 'p' buffer size is 10 [ctuPointerArith]\n"
5528+
"[test.cpp:6:6]: note: Calling function g, 1st argument is accessed out of bounds\n"
5529+
"[test.cpp:2:12]: note: Using argument p\n",
5530+
errout_str());
55185531
}
55195532

55205533
void objectIndex() {

0 commit comments

Comments
 (0)