From 4f88ff489cd5c1db8fd514c9cdb90c2b4d1957b1 Mon Sep 17 00:00:00 2001 From: Lukasz Stanislawski Date: Thu, 16 Aug 2018 16:05:04 +0200 Subject: procfs: check stream error Theoretically there might be a situation when stream error occured but procfs_read_process_cmdline func will return 0 (no error) and NULL as cmd line. This patch prevents such situation by returning error code in such scenarios. Change-Id: I7b02dd50fbd7a17ab8ba7cdafb28ea5e785bb22a --- src/procfs.c | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/procfs.c b/src/procfs.c index 7c81e53..673328b 100644 --- a/src/procfs.c +++ b/src/procfs.c @@ -409,29 +409,32 @@ int procfs_read_process_cmdline(int pid, char **cmdline) while ((read = fread(buf, 1, sizeof(buf), cmdline_fp)) != 0) { char *tmp = realloc(cmd, read_total + read + 1); if (!tmp) { - fclose(cmdline_fp); - free(cmd); - return -1; + goto error; } cmd = tmp; memcpy(cmd + read_total, buf, read); read_total += read; } - if (read_total == 0) { - fclose(cmdline_fp); - *cmdline = NULL; - return 0; + if (ferror(cmdline_fp)) { + goto error; } // clear string from end-of-string characters - for (int i = 0; i < read_total; ++i) { - if (cmd[i] == '\0') - cmd[i] = ' '; + if (read_total > 0) { + for (int i = 0; i < read_total; ++i) { + if (cmd[i] == '\0') + cmd[i] = ' '; + } + cmd[read_total] = '\0'; } - cmd[read_total] = '\0'; - fclose(cmdline_fp); *cmdline = cmd; + fclose(cmdline_fp); return 0; + +error: + fclose(cmdline_fp); + free(cmd); + return -1; } -- cgit v1.2.3