diff options
author | Colin Ian King <colin.king@canonical.com> | 2014-02-27 18:40:32 +0000 |
---|---|---|
committer | Alexandra Yates <alexandra.yates@linux.intel.com> | 2014-04-02 18:48:18 -0700 |
commit | d832fb930d3c80fb502a20257923d0a65e9880de (patch) | |
tree | 9c93a2d9a2580d06f2b279315695dbc9cfe3df95 /traceevent/event-parse.c | |
parent | 3c07c386168d8e5f1043771f65fbe298d3ce9936 (diff) |
event-parse: fix common realloc out of memory freeing issue
When realloc fails one must not assume it has free'd the original
allocated memory, so one should free this first before bailing out
on the error exit path. For example, see section 7.20.3.4 of the
C11 standard.
Signed-off-by: Colin Ian King <colin.king@canonical.com>
Diffstat (limited to 'traceevent/event-parse.c')
-rw-r--r-- | traceevent/event-parse.c | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/traceevent/event-parse.c b/traceevent/event-parse.c index f63a262..84ac24d 100644 --- a/traceevent/event-parse.c +++ b/traceevent/event-parse.c @@ -230,7 +230,7 @@ int pevent_pid_is_registered(struct pevent *pevent, int pid) */ static int add_new_comm(struct pevent *pevent, const char *comm, int pid) { - struct cmdline *cmdlines = pevent->cmdlines; + struct cmdline *newcmdlines, *cmdlines = pevent->cmdlines; const struct cmdline *cmdline; struct cmdline key; @@ -247,11 +247,13 @@ static int add_new_comm(struct pevent *pevent, const char *comm, int pid) return -1; } - cmdlines = realloc(cmdlines, sizeof(*cmdlines) * (pevent->cmdline_count + 1)); - if (!cmdlines) { + newcmdlines = realloc(cmdlines, sizeof(*cmdlines) * (pevent->cmdline_count + 1)); + if (!newcmdlines) { + free(cmdlines); errno = ENOMEM; return -1; } + cmdlines = newcmdlines; cmdlines[pevent->cmdline_count].comm = strdup(comm); if (!cmdlines[pevent->cmdline_count].comm) { |