Skip to content

Commit 2fd3f2f

Browse files
HurryPenghebasto
authored andcommitted
subprocess: Fix memory leaks
I encountered this issue while running my code with Valgrind today. Below is part of the Valgrind error message: ``` ==1578139== 472 bytes in 1 blocks are still reachable in loss record 1 of 1 ==1578139== at 0x4848899: malloc (...) ==1578139== by 0x4B3AF62: fdopen@@GLIBC_2.2.5 (...) ==1578139== by 0x118B09: subprocess::Popen::execute_process() (...) ``` I noticed that a similar fix had been proposed by another contributor previously. I did not mean to scoop their work, but merely hoping to fix it sooner so other people don't get confused by it just as I did today. Github-Pull: arun11299/cpp-subprocess#106 Rebased-From: 3afe581c1f22f106d59cf54b9b65251e6c554671
1 parent 5b8046a commit 2fd3f2f

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/util/subprocess.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,11 +1181,13 @@ inline void Popen::execute_process() noexcept(false)
11811181
try {
11821182
char err_buf[SP_MAX_ERR_BUF_SIZ] = {0,};
11831183

1184-
int read_bytes = util::read_atmost_n(
1185-
fdopen(err_rd_pipe, "r"),
1186-
err_buf,
1187-
SP_MAX_ERR_BUF_SIZ);
1188-
close(err_rd_pipe);
1184+
FILE* err_fp = fdopen(err_rd_pipe, "r");
1185+
if (!err_fp) {
1186+
close(err_rd_pipe);
1187+
throw OSError("fdopen failed", errno);
1188+
}
1189+
int read_bytes = util::read_atmost_n(err_fp, err_buf, SP_MAX_ERR_BUF_SIZ);
1190+
fclose(err_fp);
11891191

11901192
if (read_bytes || strlen(err_buf)) {
11911193
// Call waitpid to reap the child process

0 commit comments

Comments
 (0)