From 59ccfeb33a06a8b7c86248924b21c1b44cd289a4 Mon Sep 17 00:00:00 2001 From: Hans-Joerg Schurr Date: Tue, 19 Mar 2024 15:04:44 -0500 Subject: [PATCH] Handle system call return values in oracle runners This handles the error case when creating the pipes and when writing to them. --- src/base/run.cpp | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/base/run.cpp b/src/base/run.cpp index 68e2f1d3..2478251d 100644 --- a/src/base/run.cpp +++ b/src/base/run.cpp @@ -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) @@ -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(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;