Skip to content

Commit ed53e76

Browse files
committed
Improve error message: remove & when operand is bufer
1 parent 70c1ddd commit ed53e76

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

lib/checkbufferoverrun.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ static const ValueFlow::Value *getBufferSizeValue(const Token *tok)
7575
return it == tokenValues.cend() ? nullptr : &*it;
7676
}
7777

78+
static const Token* getRealBufferTok(const Token* tok) {
79+
if (!tok->isUnaryOp("&"))
80+
return tok;
81+
82+
const auto* op = tok->astOperand1();
83+
return (op->valueType() && op->valueType()->pointer) ? op : tok;
84+
}
85+
7886
static int getMinFormatStringOutputLength(const std::vector<const Token*> &parameters, nonneg int formatStringArgNr)
7987
{
8088
if (formatStringArgNr <= 0 || formatStringArgNr > parameters.size())
@@ -690,7 +698,7 @@ void CheckBufferOverrun::bufferOverflow()
690698

691699
void CheckBufferOverrun::bufferOverflowError(const Token *tok, const ValueFlow::Value *value, Certainty certainty)
692700
{
693-
reportError(getErrorPath(tok, value, "Buffer overrun"), Severity::error, "bufferAccessOutOfBounds", "Buffer is accessed out of bounds: " + (tok ? tok->expressionString() : "buf"), CWE_BUFFER_OVERRUN, certainty);
701+
reportError(getErrorPath(tok, value, "Buffer overrun"), Severity::error, "bufferAccessOutOfBounds", "Buffer is accessed out of bounds: " + (tok ? getRealBufferTok(tok)->expressionString() : "buf"), CWE_BUFFER_OVERRUN, certainty);
694702
}
695703

696704
//---------------------------------------------------------------------------
@@ -800,7 +808,7 @@ void CheckBufferOverrun::stringNotZeroTerminated()
800808
if (isZeroTerminated)
801809
continue;
802810
// TODO: Locate unsafe string usage..
803-
terminateStrncpyError(tok, args[0]->expressionString());
811+
terminateStrncpyError(tok, getRealBufferTok(args[0])->expressionString());
804812
}
805813
}
806814
}

test/testbufferoverrun.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5723,6 +5723,12 @@ class TestBufferOverrun : public TestFixture {
57235723
"}");
57245724
ASSERT_EQUALS("[test.cpp:3:10]: (error) Buffer is accessed out of bounds: &i [bufferAccessOutOfBounds]\n", errout_str());
57255725

5726+
check("void f() {\n"
5727+
" int i[2];\n"
5728+
" memset(&i, 0, 1000);\n"
5729+
"}");
5730+
ASSERT_EQUALS("[test.cpp:3:10]: (error) Buffer is accessed out of bounds: i [bufferAccessOutOfBounds]\n", errout_str());
5731+
57265732
check("void f() {\n"
57275733
" int i;\n"
57285734
" memset(&i, 0, sizeof(i));\n"
@@ -5733,7 +5739,7 @@ class TestBufferOverrun : public TestFixture {
57335739
" char c[6];\n"
57345740
" strncpy(&c, \"hello!\", 6);\n"
57355741
"}");
5736-
ASSERT_EQUALS("[test.cpp:3:3]: (warning, inconclusive) The buffer '&c' may not be null-terminated after the call to strncpy(). [terminateStrncpy]\n", errout_str());
5742+
ASSERT_EQUALS("[test.cpp:3:3]: (warning, inconclusive) The buffer 'c' may not be null-terminated after the call to strncpy(). [terminateStrncpy]\n", errout_str());
57375743

57385744
check("void foo() {\n"
57395745
" char c[6];\n"

0 commit comments

Comments
 (0)