diff options
author | Arnaldo Carvalho de Melo <acme@redhat.com> | 2010-05-18 22:04:28 -0400 |
---|---|---|
committer | Arnaldo Carvalho de Melo <acme@redhat.com> | 2010-05-18 22:04:28 -0400 |
commit | b448c4b613542c16ad66042017946e68da4e422b (patch) | |
tree | 31f351ef2875a2ae749a6ae765b1f66820716b96 /tools/perf | |
parent | a41794cdd7ee94a5199e14f642c26d649d383fa5 (diff) |
perf probe: Fix some error exit paths
That could leave filedescriptors open and leak memory. Also stop using
xmalloc, use malloc and handle results just like other error cases in
the same routine that used it.
Cc: Frédéric Weisbecker <fweisbec@gmail.com>
Cc: Masami Hiramatsu <mhiramat@redhat.com>
Cc: Mike Galbraith <efault@gmx.de>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Tom Zanussi <tzanussi@gmail.com>
LKML-Reference: <new-submission>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools/perf')
-rw-r--r-- | tools/perf/util/probe-finder.c | 29 |
1 files changed, 19 insertions, 10 deletions
diff --git a/tools/perf/util/probe-finder.c b/tools/perf/util/probe-finder.c index 93583eb19cd6..d964cb199c67 100644 --- a/tools/perf/util/probe-finder.c +++ b/tools/perf/util/probe-finder.c | |||
@@ -743,32 +743,36 @@ static int find_lazy_match_lines(struct list_head *head, | |||
743 | const char *fname, const char *pat) | 743 | const char *fname, const char *pat) |
744 | { | 744 | { |
745 | char *fbuf, *p1, *p2; | 745 | char *fbuf, *p1, *p2; |
746 | int fd, ret, line, nlines = 0; | 746 | int fd, line, nlines = -1; |
747 | struct stat st; | 747 | struct stat st; |
748 | 748 | ||
749 | fd = open(fname, O_RDONLY); | 749 | fd = open(fname, O_RDONLY); |
750 | if (fd < 0) { | 750 | if (fd < 0) { |
751 | pr_warning("Failed to open %s: %s\n", fname, strerror(-fd)); | 751 | pr_warning("Failed to open %s: %s\n", fname, strerror(-fd)); |
752 | return fd; | 752 | return -errno; |
753 | } | 753 | } |
754 | 754 | ||
755 | ret = fstat(fd, &st); | 755 | if (fstat(fd, &st) < 0) { |
756 | if (ret < 0) { | ||
757 | pr_warning("Failed to get the size of %s: %s\n", | 756 | pr_warning("Failed to get the size of %s: %s\n", |
758 | fname, strerror(errno)); | 757 | fname, strerror(errno)); |
759 | return ret; | 758 | nlines = -errno; |
759 | goto out_close; | ||
760 | } | 760 | } |
761 | fbuf = xmalloc(st.st_size + 2); | 761 | |
762 | ret = read(fd, fbuf, st.st_size); | 762 | nlines = -ENOMEM; |
763 | if (ret < 0) { | 763 | fbuf = malloc(st.st_size + 2); |
764 | if (fbuf == NULL) | ||
765 | goto out_close; | ||
766 | if (read(fd, fbuf, st.st_size) < 0) { | ||
764 | pr_warning("Failed to read %s: %s\n", fname, strerror(errno)); | 767 | pr_warning("Failed to read %s: %s\n", fname, strerror(errno)); |
765 | return ret; | 768 | nlines = -errno; |
769 | goto out_free_fbuf; | ||
766 | } | 770 | } |
767 | close(fd); | ||
768 | fbuf[st.st_size] = '\n'; /* Dummy line */ | 771 | fbuf[st.st_size] = '\n'; /* Dummy line */ |
769 | fbuf[st.st_size + 1] = '\0'; | 772 | fbuf[st.st_size + 1] = '\0'; |
770 | p1 = fbuf; | 773 | p1 = fbuf; |
771 | line = 1; | 774 | line = 1; |
775 | nlines = 0; | ||
772 | while ((p2 = strchr(p1, '\n')) != NULL) { | 776 | while ((p2 = strchr(p1, '\n')) != NULL) { |
773 | *p2 = '\0'; | 777 | *p2 = '\0'; |
774 | if (strlazymatch(p1, pat)) { | 778 | if (strlazymatch(p1, pat)) { |
@@ -778,7 +782,10 @@ static int find_lazy_match_lines(struct list_head *head, | |||
778 | line++; | 782 | line++; |
779 | p1 = p2 + 1; | 783 | p1 = p2 + 1; |
780 | } | 784 | } |
785 | out_free_fbuf: | ||
781 | free(fbuf); | 786 | free(fbuf); |
787 | out_close: | ||
788 | close(fd); | ||
782 | return nlines; | 789 | return nlines; |
783 | } | 790 | } |
784 | 791 | ||
@@ -955,6 +962,8 @@ int find_kprobe_trace_events(int fd, struct perf_probe_event *pev, | |||
955 | if (!dbg) { | 962 | if (!dbg) { |
956 | pr_warning("No dwarf info found in the vmlinux - " | 963 | pr_warning("No dwarf info found in the vmlinux - " |
957 | "please rebuild with CONFIG_DEBUG_INFO=y.\n"); | 964 | "please rebuild with CONFIG_DEBUG_INFO=y.\n"); |
965 | free(pf.tevs); | ||
966 | *tevs = NULL; | ||
958 | return -EBADF; | 967 | return -EBADF; |
959 | } | 968 | } |
960 | 969 | ||