Skip to content

Commit

Permalink
Merge pull request #29 from hansjoergschurr/devel/run-return
Browse files Browse the repository at this point in the history
This handles the return values of the system calls that setup the pipes and that writes into the pipe. A non-zero return value indicates an error.
To keep things simple, an incomplete write to the pipe is also considered an error.

This should stop the warnings here.
  • Loading branch information
ajreynol authored Mar 19, 2024
2 parents 0b21354 + 59ccfeb commit c934e42
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/base/run.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,15 @@ int run(const std::string& call,
{
int read_pipe[2];
int write_pipe[2];
pipe(read_pipe);
pipe(write_pipe);
if (pipe(read_pipe))
{
// Creating the pipe failed.
return -1;
}
if (pipe(write_pipe))
{
return -1;
}

pid_t pid = fork();
if (pid == -1)
Expand Down Expand Up @@ -60,7 +67,17 @@ int run(const std::string& call,
close(write_pipe[0]);
close(read_pipe[1]);

write(write_pipe[1], content.c_str(), content.length() + 1);
ssize_t written =
write(write_pipe[1], content.c_str(), content.length() + 1);
// Error cases: -1 is explicit error, or it's an incomplete write.
// Second case we could write more bytes, but we are writing to a pipe. So
// something odd must have happened.
if (written == -1 || written != static_cast<ssize_t>(content.length()) + 1)
{
close(write_pipe[1]);
close(read_pipe[0]);
return -1;
}
// Wait for child and get return code
int status;
pid_t ret;
Expand Down

0 comments on commit c934e42

Please sign in to comment.