summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLukasz Stanislawski <l.stanislaws@samsung.com>2018-08-16 16:05:04 +0200
committerLukasz Stanislawski <l.stanislaws@samsung.com>2018-08-16 14:57:48 +0000
commit4f88ff489cd5c1db8fd514c9cdb90c2b4d1957b1 (patch)
treeed691a880c05bbeeeb801de9cd0102ec8d212f99
parent25931837720d5a496d202b02697d5d7abbf2d03e (diff)
downloadttsd-worker-task-4f88ff489cd5c1db8fd514c9cdb90c2b4d1957b1.tar.gz
ttsd-worker-task-4f88ff489cd5c1db8fd514c9cdb90c2b4d1957b1.tar.bz2
ttsd-worker-task-4f88ff489cd5c1db8fd514c9cdb90c2b4d1957b1.zip
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
-rw-r--r--src/procfs.c27
1 files 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;
}