Skip to contentSkip to conversation

Fix double fclose() on do_write(fd, xxx) failure

PATCH In review Thomas Jarosch 1 participant 2 messages Jan 28, 2013 – Jan 31, 2013 perf events

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 Fix double fclose() on do_write(fd, xxx) failure

cppcheck reported:
[util/header.c:983]: (error) Used file that is not opened.

Thanks to Arnaldo Carvalho de Melo for pointing out that fclose(NULL) is undefined behavior -> protect against it.

---
 tools/perf/util/header.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/perf/util/header.c +3 -1
index 7b24cf3..f6081cb 100644
@@ -955,6 +955,7 @@ static int write_topo_node(int fd, int node)
 	}
 
 	fclose(fp);
+	fp = NULL;
 
 	ret = do_write(fd, &mem_total, sizeof(u64));
 	if (ret)
@@ -981,7 +982,8 @@ static int write_topo_node(int fd, int node)
 	ret = do_write_string(fd, buf);
 done:
 	free(buf);
-	fclose(fp);
+	if (fp)
+		fclose(fp);
 	return ret;
 }
 
Signature · 1 line
1.7.11.7
#1 lore raw

[tip:perf/core] perf header: Fix double fclose() on do_write(fd, xxx) failure

Commit-ID:  5809fde040de2afa477a6c593ce2e8fd2c11d9d3
Gitweb:     http://git.kernel.org/tip/5809fde040de2afa477a6c593ce2e8fd2c11d9d3
Author:     Thomas Jarosch <[email protected]>
AuthorDate: Mon, 28 Jan 2013 10:21:14 +0100
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Wed, 30 Jan 2013 10:40:44 -0300

perf header: Fix double fclose() on do_write(fd, xxx) failure

cppcheck reported:
[util/header.c:983]: (error) Used file that is not opened.

Thanks to Arnaldo Carvalho de Melo for pointing out that fclose(NULL) is undefined behavior -> protect against it.

---
 tools/perf/util/header.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tools/perf/util/header.c +3 -1
index 7b24cf3..f6081cb 100644
@@ -955,6 +955,7 @@ static int write_topo_node(int fd, int node)
 	}
 
 	fclose(fp);
+	fp = NULL;
 
 	ret = do_write(fd, &mem_total, sizeof(u64));
 	if (ret)
@@ -981,7 +982,8 @@ static int write_topo_node(int fd, int node)
 	ret = do_write_string(fd, buf);
 done:
 	free(buf);
-	fclose(fp);
+	if (fp)
+		fclose(fp);
 	return ret;
 }