Skip to content

Commit

Permalink
ProcessExecutor: removed unnecessary \0 passing in message reading/…
Browse files Browse the repository at this point in the history
…writing - fixes stray `\0` characters in output (danmar#5354)

This was introduced in danmar#5279. We were transferring the terminating `\0`
via the pipe and also added another one in the parsing. As we are now
directly writing into a `std::string` these extra characters will now
show up in it. So just get rid of them.
  • Loading branch information
firewave authored Aug 21, 2023
1 parent 3281fc9 commit 59c3bd2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
7 changes: 2 additions & 5 deletions cli/processexecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class PipeWriter : public ErrorLogger {
writeToPipeInternal(type, &t, 1);
}

const unsigned int len = static_cast<unsigned int>(data.length() + 1);
const unsigned int len = static_cast<unsigned int>(data.length());
{
static constexpr std::size_t l_size = sizeof(unsigned int);
writeToPipeInternal(type, &len, l_size);
Expand Down Expand Up @@ -162,9 +162,7 @@ bool ProcessExecutor::handleRead(int rpipe, unsigned int &result, const std::str
std::exit(EXIT_FAILURE);
}

// Don't rely on incoming data being null-terminated.
// Allocate +1 element and null-terminate the buffer.
std::string buf(len + 1, '\0');
std::string buf(len, '\0');
char *data_start = &buf[0];
bytes_to_read = len;
do {
Expand All @@ -177,7 +175,6 @@ bool ProcessExecutor::handleRead(int rpipe, unsigned int &result, const std::str
bytes_to_read -= bytes_read;
data_start += bytes_read;
} while (bytes_to_read != 0);
buf[len] = '\0';

bool res = true;
if (type == PipeWriter::REPORT_OUT) {
Expand Down
11 changes: 11 additions & 0 deletions test/cli/test-other.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,15 @@ def test_invalid_library(tmpdir):
"cppcheck: Failed to load library configuration file 'none2'. File not found\n")
assert stderr == ""


def test_message_j(tmpdir):
test_file = os.path.join(tmpdir, 'test.c')
with open(test_file, 'wt') as f:
f.write("")

args = ['-j2', '--platform=native', test_file]

_, stdout, _ = cppcheck(args)
assert stdout == "Checking {} ...\n".format(test_file) # we were adding stray \0 characters at the end

# TODO: test missing std.cfg

0 comments on commit 59c3bd2

Please sign in to comment.