Skip to contentSkip to conversation

perf probe: Fix to close target file

PATCH Superseded Masami Hiramatsu 3 participants 4 messages Apr 26, 2016 – Apr 27, 2016 perf events

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf probe: Fix to close target file

Fix a bug to close target elf file in get_text_start_address().

---
 tools/perf/util/probe-event.c |    1 +
 1 file changed, 1 insertion(+)
tools/perf/util/probe-event.c +1 -0
index 8319fbb..258b49d 100644
@@ -499,6 +499,7 @@ static int get_text_start_address(const char *exec, unsigned long *address)
 	ret = 0;
 out:
 	elf_end(elf);
+	close(fd);
 	return ret;
 }
 
#1 lore raw

PATCH v2 1/1 perf probe: Fix to close target file

Fix a bug to close target elf file in get_text_start_address().

---
Changes in v2:
- handle an error case in elf_begin() too.

---
 tools/perf/util/probe-event.c |    9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
tools/perf/util/probe-event.c +7 -2
index 8319fbb..97b7f8e 100644
@@ -486,8 +486,10 @@ static int get_text_start_address(const char *exec, unsigned long *address)
 		return -errno;
 
 	elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
-	if (elf == NULL)
-		return -EINVAL;
+	if (elf == NULL) {
+		ret = -EINVAL;
+		goto out_close;
+	}
 
 	if (gelf_getehdr(elf, &ehdr) == NULL)
 		goto out;
@@ -499,6 +501,9 @@ static int get_text_start_address(const char *exec, unsigned long *address)
 	ret = 0;
 out:
 	elf_end(elf);
+out_close:
+	close(fd);
+
 	return ret;
 }
 
#3 lore raw

[tip:perf/core] perf probe: Close target file on error path

In reply to Masami Hiramatsu
perf probe: Close target file on error path
Commit-ID:  062d6c2aec0e087be956494a73221c04eca115fe
Gitweb:     http://git.kernel.org/tip/062d6c2aec0e087be956494a73221c04eca115fe
Author:     Masami Hiramatsu <[email protected]>
AuthorDate: Tue, 26 Apr 2016 15:47:37 +0900
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Tue, 26 Apr 2016 10:56:08 -0300

perf probe: Close target file on error path

Fix a bug to close target elf file in get_text_start_address().

---
 tools/perf/util/probe-event.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
tools/perf/util/probe-event.c +7 -2
index 8319fbb..97b7f8e 100644
@@ -486,8 +486,10 @@ static int get_text_start_address(const char *exec, unsigned long *address)
 		return -errno;
 
 	elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
-	if (elf == NULL)
-		return -EINVAL;
+	if (elf == NULL) {
+		ret = -EINVAL;
+		goto out_close;
+	}
 
 	if (gelf_getehdr(elf, &ehdr) == NULL)
 		goto out;
@@ -499,6 +501,9 @@ static int get_text_start_address(const char *exec, unsigned long *address)
 	ret = 0;
 out:
 	elf_end(elf);
+out_close:
+	close(fd);
+
 	return ret;
 }