Skip to contentSkip to conversation

Use pclose() instead of fclose() on pipe stream

PATCH New Thomas Jarosch 1 participant 3 messages Jan 25, 2013 – Jan 31, 2013 perf events

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 Use pclose() instead of fclose() on pipe stream

cppcheck message:
[tools/perf/util/sort.c:277]: (error) Mismatching allocation and deallocation: fp

Also fix descriptor leak on error and always initialize the "fp" variable.

---
 tools/perf/util/sort.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/perf/util/sort.c +3 -2
index 7ad6239..a83ded2 100644
@@ -249,7 +249,7 @@ static int hist_entry__srcline_snprintf(struct hist_entry *self, char *bf,
 					size_t size,
 					unsigned int width __maybe_unused)
 {
-	FILE *fp;
+	FILE *fp = NULL;
 	char cmd[PATH_MAX + 2], *path = self->srcline, *nl;
 	size_t line_len;
 
@@ -270,7 +270,6 @@ static int hist_entry__srcline_snprintf(struct hist_entry *self, char *bf,
 
 	if (getline(&path, &line_len, fp) < 0 || !line_len)
 		goto out_ip;
-	fclose(fp);
 	self->srcline = strdup(path);
 	if (self->srcline == NULL)
 		goto out_ip;
@@ -280,8 +279,10 @@ static int hist_entry__srcline_snprintf(struct hist_entry *self, char *bf,
 		*nl = '\0';
 	path = self->srcline;
 out_path:
+	pclose(fp);
 	return repsep_snprintf(bf, size, "%s", path);
 out_ip:
+	pclose(fp);
 	return repsep_snprintf(bf, size, "%-#*llx", BITS_PER_LONG / 4, self->ip);
 }
 
Signature · 1 line
1.7.11.7
#1 lore raw

PATCH 1/1 Fix NULL pointer dereference on error

pclose(NULL) is undefined behavior and crashes.

---
 tools/perf/util/sort.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/perf/util/sort.c +4 -2
index a83ded2..8333661 100644
@@ -279,10 +279,12 @@ static int hist_entry__srcline_snprintf(struct hist_entry *self, char *bf,
 		*nl = '\0';
 	path = self->srcline;
 out_path:
-	pclose(fp);
+	if (fp)
+		pclose(fp);
 	return repsep_snprintf(bf, size, "%s", path);
 out_ip:
-	pclose(fp);
+	if (fp)
+		pclose(fp);
 	return repsep_snprintf(bf, size, "%-#*llx", BITS_PER_LONG / 4, self->ip);
 }
 
Signature · 1 line
1.7.11.7
#2 lore raw

[tip:perf/core] perf sort: Use pclose() instead of fclose() on pipe stream

Commit-ID:  8eb44dd76ac994b020e5cfe72635c90d9e0ad995
Gitweb:     http://git.kernel.org/tip/8eb44dd76ac994b020e5cfe72635c90d9e0ad995
Author:     Thomas Jarosch <[email protected]>
AuthorDate: Fri, 25 Jan 2013 11:02:13 +0100
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Wed, 30 Jan 2013 10:38:48 -0300

perf sort: Use pclose() instead of fclose() on pipe stream

cppcheck message:
[tools/perf/util/sort.c:277]: (error) Mismatching allocation and deallocation: fp

Also fix descriptor leak on error and always initialize the "fp" variable.

---
 tools/perf/util/sort.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
tools/perf/util/sort.c +5 -2
index 7ad6239..8333661 100644
@@ -249,7 +249,7 @@ static int hist_entry__srcline_snprintf(struct hist_entry *self, char *bf,
 					size_t size,
 					unsigned int width __maybe_unused)
 {
-	FILE *fp;
+	FILE *fp = NULL;
 	char cmd[PATH_MAX + 2], *path = self->srcline, *nl;
 	size_t line_len;
 
@@ -270,7 +270,6 @@ static int hist_entry__srcline_snprintf(struct hist_entry *self, char *bf,
 
 	if (getline(&path, &line_len, fp) < 0 || !line_len)
 		goto out_ip;
-	fclose(fp);
 	self->srcline = strdup(path);
 	if (self->srcline == NULL)
 		goto out_ip;
@@ -280,8 +279,12 @@ static int hist_entry__srcline_snprintf(struct hist_entry *self, char *bf,
 		*nl = '\0';
 	path = self->srcline;
 out_path:
+	if (fp)
+		pclose(fp);
 	return repsep_snprintf(bf, size, "%s", path);
 out_ip:
+	if (fp)
+		pclose(fp);
 	return repsep_snprintf(bf, size, "%-#*llx", BITS_PER_LONG / 4, self->ip);
 }