diff options
author | dec05eba <dec05eba@protonmail.com> | 2020-10-23 08:39:15 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-10-23 09:11:51 +0200 |
commit | 96c9ed391270347c4c7036179fd2815f679ca7cf (patch) | |
tree | 8dac4bd431bf57a3391d0aeb802aa34704043b4b /src/Program.c | |
parent | 8f73d303b96d35ccec9856fc0043a74c8800536b (diff) |
Kill program on read failure or callback returning non-0 value, fix potential crash
Diffstat (limited to 'src/Program.c')
-rw-r--r-- | src/Program.c | 13 |
1 files changed, 9 insertions, 4 deletions
diff --git a/src/Program.c b/src/Program.c index fc80e5e..a82bcd2 100644 --- a/src/Program.c +++ b/src/Program.c @@ -74,14 +74,20 @@ int exec_program(const char **args, ProgramOutputCallback output_callback, void int err = errno; fprintf(stderr, "Failed to read from pipe to program %s, error: %s\n", args[0], strerror(err)); result = -err; - goto cleanup; + break; } buffer[bytes_read] = '\0'; - if(output_callback && output_callback(buffer, bytes_read, userdata) != 0) - break; + if(output_callback) { + result = output_callback(buffer, bytes_read, userdata); + if(result != 0) + break; + } } + if(result != 0) + kill(read_program.pid, SIGTERM); + if(waitpid(read_program.pid, &status, 0) == -1) { perror("waitpid failed"); result = -5; @@ -105,7 +111,6 @@ int exec_program(const char **args, ProgramOutputCallback output_callback, void } fprintf(stderr, "), exit status %d\n", exit_status); result = -exit_status; - goto cleanup; } cleanup: |