Skip to content

Commit

Permalink
perf probe: Check for dup and fdopen failures
Browse files Browse the repository at this point in the history
dup and fdopen can potentially fail, so add some extra
error handling checks rather than assuming they always work.

Signed-off-by: Colin King <colin.king@canonical.com>
Acked-by: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Wang Nan <wangnan0@huawei.com>
Link: http://lkml.kernel.org/r/1471038296-12956-1-git-send-email-colin.king@canonical.com
[ Free resources when those functions (now being verified) fail ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
  • Loading branch information
Colin Ian King authored and Arnaldo Carvalho de Melo committed Aug 15, 2016
1 parent 50de1a0 commit 0325862
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions tools/perf/util/probe-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ int probe_file__open_both(int *kfd, int *ufd, int flag)
/* Get raw string list of current kprobe_events or uprobe_events */
struct strlist *probe_file__get_rawlist(int fd)
{
int ret, idx;
int ret, idx, fddup;
FILE *fp;
char buf[MAX_CMDLEN];
char *p;
Expand All @@ -144,7 +144,14 @@ struct strlist *probe_file__get_rawlist(int fd)

sl = strlist__new(NULL, NULL);

fp = fdopen(dup(fd), "r");
fddup = dup(fd);
if (fddup < 0)
goto out_free_sl;

fp = fdopen(fddup, "r");
if (!fp)
goto out_close_fddup;

while (!feof(fp)) {
p = fgets(buf, MAX_CMDLEN, fp);
if (!p)
Expand All @@ -163,6 +170,12 @@ struct strlist *probe_file__get_rawlist(int fd)
fclose(fp);

return sl;

out_close_fddup:
close(fddup);
out_free_sl:
strlist__delete(sl);
return NULL;
}

static struct strlist *__probe_file__get_namelist(int fd, bool include_group)
Expand Down Expand Up @@ -447,10 +460,13 @@ static int probe_cache__load(struct probe_cache *pcache)
{
struct probe_cache_entry *entry = NULL;
char buf[MAX_CMDLEN], *p;
int ret = 0;
int ret = 0, fddup;
FILE *fp;

fp = fdopen(dup(pcache->fd), "r");
fddup = dup(pcache->fd);
if (fddup < 0)
return -errno;
fp = fdopen(fddup, "r");
if (!fp)
return -EINVAL;

Expand Down

0 comments on commit 0325862

Please sign in to comment.