Improved pointer arithmetic message

This commit is contained in:
Daniel Marjamäki 2014-12-25 14:31:46 +01:00
parent bc594d52c8
commit 7ab12cea63
2 changed files with 14 additions and 7 deletions

View File

@ -179,9 +179,16 @@ void CheckBufferOverrun::pointerOutOfBoundsError(const Token *tok, const Token *
// cause bad behaviour on most implementations. people create out
// of bounds pointers by intention.
const std::string expr(tok ? tok->expressionString() : std::string(""));
std::string errmsg("Undefined behaviour. Pointer arithmetic '" + expr + "' result is out of bounds");
if (index && !index->isNumber())
errmsg += " when " + index->expressionString() + " is " + MathLib::toString(indexvalue);
std::string errmsg;
if (index && !index->isNumber()) {
errmsg = "Undefined behaviour, when '" +
index->expressionString() +
"' is " +
MathLib::toString(indexvalue) +
" the pointer arithmetic '" + expr + "' is out of bounds";
} else {
errmsg = "Undefined behaviour, pointer arithmetic '" + expr + "' is out of bounds";
}
std::string verbosemsg(errmsg + ". From chapter 6.5.6 in the C specification:\n"
"\"When an expression that has integer type is added to or subtracted from a pointer, ..\" and then \"If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined.\"");
reportError(tok, Severity::portability, "pointerOutOfBounds", errmsg + ".\n" + verbosemsg);

View File

@ -2962,13 +2962,13 @@ private:
" char a[10];\n"
" char *p = a + 100;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (portability) Undefined behaviour. Pointer arithmetic 'a+100' result is out of bounds.\n", errout.str());
ASSERT_EQUALS("[test.cpp:3]: (portability) Undefined behaviour, pointer arithmetic 'a+100' is out of bounds.\n", errout.str());
check("void f() {\n"
" char a[10];\n"
" return a + 100;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (portability) Undefined behaviour. Pointer arithmetic 'a+100' result is out of bounds.\n", errout.str());
ASSERT_EQUALS("[test.cpp:3]: (portability) Undefined behaviour, pointer arithmetic 'a+100' is out of bounds.\n", errout.str());
check("void f() {\n" // #6350 - fp when there is cast of buffer
" wchar_t buf[64];\n"
@ -2983,7 +2983,7 @@ private:
" p += 100;\n"
" free(p);"
"}");
ASSERT_EQUALS("[test.cpp:3]: (portability) Undefined behaviour. Pointer arithmetic 'p+100' result is out of bounds.\n", errout.str());
ASSERT_EQUALS("[test.cpp:3]: (portability) Undefined behaviour, pointer arithmetic 'p+100' is out of bounds.\n", errout.str());
check("void f() {\n"
" char *p = malloc(10);\n"
@ -3017,7 +3017,7 @@ private:
" char x[10];\n"
" return x-1;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (portability) Undefined behaviour. Pointer arithmetic 'x-1' result is out of bounds.\n", errout.str());
ASSERT_EQUALS("[test.cpp:3]: (portability) Undefined behaviour, pointer arithmetic 'x-1' is out of bounds.\n", errout.str());
}
void sprintf1() {