Resolve CID 1379700

Ensure the string is null-terminated
This commit is contained in:
Dmitry-Me 2017-08-21 18:25:12 +03:00
parent b1b82319b1
commit 2820febaff
1 changed files with 6 additions and 2 deletions

View File

@ -102,11 +102,15 @@ int ThreadExecutor::handleRead(int rpipe, unsigned int &result)
std::exit(0);
}
char *buf = new char[len];
if (read(rpipe, buf, len) <= 0) {
// Don't rely on incoming data being null-terminated.
// Allocate +1 element and null-terminate the buffer.
char *buf = new char[len + 1];
const ssize_t readIntoBuf = read(rpipe, buf, len);
if (readIntoBuf <= 0) {
std::cerr << "#### You found a bug from cppcheck.\nThreadExecutor::handleRead error, type was:" << type << std::endl;
std::exit(0);
}
buf[readIntoBuf] = 0;
if (type == REPORT_OUT) {
_errorLogger.reportOut(buf);