Skip to contentSkip to conversation

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

PATCH In review Thomas Jarosch 2 participants 3 messages Jan 25, 2013 – Jan 28, 2013 perf events

lore
Share & tools
lore
#0 lore raw

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

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

---
 tools/perf/util/header.c | 1 +
 1 file changed, 1 insertion(+)
tools/perf/util/header.c +1 -0
index 7b24cf3..30745c2 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)
Signature · 1 line
1.7.11.7
#1 lore raw

Em Fri, Jan 25, 2013 at 11:22:17AM +0100, Thomas Jarosch escreveu:

Quoted 13 lines

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

---
 tools/perf/util/header.c | 1 +
 1 file changed, 1 insertion(+)
tools/perf/util/header.c +0 -0
index 7b24cf3..30745c2 100644

tools/perf/util/header.c static int write_topo_node(int fd, int node) · line 958

Quoted 4 lines
tools/perf/util/header.c +1 -0
@@ -955,6 +955,7 @@ static int write_topo_node(int fd, int node)
 	}
 
 	fclose(fp);
+	fp = NULL;

Ok, but what is the behaviour of fclose(NULL)? Is it defined?

- Arnaldo

#2 lore raw
Quoted 18 lines · On Friday, 25. January 2013 13:01:51 you wrote:

Em Fri, Jan 25, 2013 at 11:22:17AM +0100, Thomas Jarosch escreveu:

Quoted 17 lines

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

---

 tools/perf/util/header.c | 1 +
 1 file changed, 1 insertion(+)
tools/perf/util/header.c +0 -0
index 7b24cf3..30745c2 100644
@@ -955,6 +955,7 @@ static int write_topo_node(int fd, int node)
 
 	}
 
Quoted 7 lines

tools/perf/util/header.c static int write_topo_node(int fd, int node)

Quoted 3 lines
tools/perf/util/header.c +1 -0
 	fclose(fp);
 
+	fp = NULL;

Ok, but what is the behaviour of fclose(NULL)? Is it defined?

- Arnaldo

Grrr, this is undefined behavior. Sorry. I always thought this is defined like free(NULL) but apparently it is not.

I'll send another version of the patch and also write to the man page maintainer to add a small hint to the fclose() man page.

Thomas