Skip to content

Commit a1b0d19

Browse files
committed
Fixed cppcheck-opensource#3688 (false positive: (inconclusive, posix) (warning) The buffer 'cBuffer' is not zero-terminated after the call to readlink().)
1 parent 12df530 commit a1b0d19

3 files changed

Lines changed: 27 additions & 17 deletions

File tree

‎lib/checkbufferoverrun.cpp‎

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1304,12 +1304,8 @@ void CheckBufferOverrun::checkScope(const Token *tok, const ArrayInfo &arrayInfo
13041304
}
13051305

13061306
// readlink() / readlinkat() buffer usage
1307-
if (_settings->standards.posix) {
1308-
if (Token::simpleMatch(tok, "readlink (") && Token::Match(tok->tokAt(2)->nextArgument(), "%varid% , %num% )", arrayInfo.declarationId()))
1309-
checkReadlinkBufferUsage(tok, scope_begin, total_size, false);
1310-
else if (Token::simpleMatch(tok, "readlinkat (") && Token::Match(tok->tokAt(2)->nextArgument()->nextArgument(), "%varid% , %num% )", arrayInfo.declarationId()))
1311-
checkReadlinkBufferUsage(tok, scope_begin, total_size, true);
1312-
}
1307+
if (_settings->standards.posix && Token::Match(tok, "readlink|readlinkat ("))
1308+
checkReadlinkBufferUsage(tok, scope_begin, arrayInfo.declarationId(), total_size);
13131309

13141310
// undefined behaviour: result of pointer arithmetic is out of bounds
13151311
if (_settings->isEnabled("portability") && Token::Match(tok, "= %varid% + %num% ;", arrayInfo.declarationId())) {
@@ -1344,12 +1340,15 @@ bool CheckBufferOverrun::isArrayOfStruct(const Token* tok, int &position)
13441340
return false;
13451341
}
13461342

1347-
void CheckBufferOverrun::checkReadlinkBufferUsage(const Token* tok, const Token *scope_begin, const MathLib::bigint total_size, const bool is_readlinkat)
1343+
void CheckBufferOverrun::checkReadlinkBufferUsage(const Token* ftok, const Token *scope_begin, const unsigned int varid, const MathLib::bigint total_size)
13481344
{
1349-
const Token* bufParam = tok->tokAt(2)->nextArgument();
1350-
if (is_readlinkat)
1351-
bufParam = bufParam->nextArgument();
1352-
const std::string funcname = is_readlinkat ? "readlinkat" : "readlink";
1345+
const std::string funcname = ftok->str();
1346+
1347+
const Token* bufParam = ftok->tokAt(2)->nextArgument();
1348+
if (funcname == "readlinkat")
1349+
bufParam = bufParam ? bufParam->nextArgument() : NULL;
1350+
if (!Token::Match(bufParam, "%varid% , %num% )", varid))
1351+
return;
13531352

13541353
const MathLib::bigint n = MathLib::toLongNumber(bufParam->strAt(2));
13551354
if (total_size > 0 && n > total_size)
@@ -1358,6 +1357,10 @@ void CheckBufferOverrun::checkReadlinkBufferUsage(const Token* tok, const Token
13581357
if (!_settings->inconclusive)
13591358
return;
13601359

1360+
// only writing a part of the buffer
1361+
if (n < total_size)
1362+
return;
1363+
13611364
// readlink()/readlinkat() never terminates the buffer, check the end of the scope for buffer termination.
13621365
bool found_termination = false;
13631366
const Token *scope_end = scope_begin->link();
@@ -1369,9 +1372,9 @@ void CheckBufferOverrun::checkReadlinkBufferUsage(const Token* tok, const Token
13691372
}
13701373

13711374
if (!found_termination) {
1372-
bufferNotZeroTerminatedError(tok, bufParam->str(), funcname);
1375+
bufferNotZeroTerminatedError(ftok, bufParam->str(), funcname);
13731376
} else if (n == total_size) {
1374-
possibleReadlinkBufferOverrunError(tok, funcname, bufParam->str());
1377+
possibleReadlinkBufferOverrunError(ftok, funcname, bufParam->str());
13751378
}
13761379
}
13771380

‎lib/checkbufferoverrun.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ class CPPCHECKLIB CheckBufferOverrun : public Check {
191191
void parse_for_body(const Token *tok2, const ArrayInfo &arrayInfo, const std::string &strindex, bool condition_out_of_bounds, unsigned int counter_varid, const std::string &min_counter_value, const std::string &max_counter_value);
192192

193193
/** Check readlink or readlinkat() buffer usage */
194-
void checkReadlinkBufferUsage(const Token *tok, const Token *scope_begin, const MathLib::bigint total_size, const bool is_readlinkat);
194+
void checkReadlinkBufferUsage(const Token *ftok, const Token *scope_begin, const unsigned int varid, const MathLib::bigint total_size);
195195

196196
/**
197197
* Helper function for checkFunctionCall - check a function parameter

‎test/testbufferoverrun.cpp‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3940,15 +3940,15 @@ class TestBufferOverrun : public TestFixture {
39403940
" ssize_t len = readlink(path, buf, 254);\n"
39413941
" printf(\"%s\n\", buf);\n"
39423942
"}");
3943-
ASSERT_EQUALS("[test.cpp:3]: (warning, inconclusive) The buffer 'buf' is not null-terminated after the call to readlink().\n", errout.str());
3943+
TODO_ASSERT_EQUALS("[test.cpp:3]: (warning, inconclusive) The buffer 'buf' is not null-terminated after the call to readlink().\n", "", errout.str());
39443944

39453945
// C only: Primitive pointer simplification
39463946
check("void f() {\n"
39473947
" char buf[255];\n"
39483948
" ssize_t len = readlink(path, &buf[0], 254);\n"
39493949
" printf(\"%s\n\", buf);\n"
39503950
"}\n", true, "test.c");
3951-
ASSERT_EQUALS("[test.c:3]: (warning, inconclusive) The buffer 'buf' is not null-terminated after the call to readlink().\n", errout.str());
3951+
TODO_ASSERT_EQUALS("[test.c:3]: (warning, inconclusive) The buffer 'buf' is not null-terminated after the call to readlink().\n", "", errout.str());
39523952

39533953
check("void f() {\n"
39543954
" char buf[255];\n"
@@ -3980,6 +3980,13 @@ class TestBufferOverrun : public TestFixture {
39803980
" buf[len] = 0;\n"
39813981
"}");
39823982
ASSERT_EQUALS("", errout.str());
3983+
3984+
check("void f() {\n"
3985+
" char buf[255] = {0};\n"
3986+
" readlink(path, buf, 254);\n" // <- doesn't write whole buf
3987+
" puts(buf);\n"
3988+
"}");
3989+
ASSERT_EQUALS("", errout.str());
39833990
}
39843991

39853992
void readlinkat() {
@@ -3988,7 +3995,7 @@ class TestBufferOverrun : public TestFixture {
39883995
" ssize_t len = readlinkat(42, path, buf, 254);\n"
39893996
" printf(\"%s\n\", buf);\n"
39903997
"}");
3991-
ASSERT_EQUALS("[test.cpp:3]: (warning, inconclusive) The buffer 'buf' is not null-terminated after the call to readlinkat().\n", errout.str());
3998+
TODO_ASSERT_EQUALS("[test.cpp:3]: (warning, inconclusive) The buffer 'buf' is not null-terminated after the call to readlinkat().\n", "", errout.str());
39923999

39934000
check("void f() {\n"
39944001
" char buf[255];\n"

0 commit comments

Comments
 (0)