diff mbox

[02/12] btrfs-progs: check fopen failure in cmds-send

Message ID 20131008171938.GL25712@lenny.home.zabbo.net (mailing list archive)
State Accepted, archived
Headers show

Commit Message

Zach Brown Oct. 8, 2013, 5:19 p.m. UTC
> > @@ -72,6 +72,11 @@ int find_mount_root(const char *path, char **mount_root)
> >  	close(fd);
> >  
> >  	mnttab = fopen("/proc/mounts", "r");
> > +	if (!mnttab) {
> > +		close(fd);
> > +		return -errno;
> 
> close() can modify errno.
> 
> And close(fd) is already called 4 lines above. You didn't run the static
> code analysis again after applying your patch :)

OK, here's a less dumb attempt:

- z

From f8a3425c184a55e0c254143e520e60a6856c27da Mon Sep 17 00:00:00 2001
From: Zach Brown <zab@redhat.com>
Date: Fri, 4 Oct 2013 15:38:18 -0700
Subject: [PATCH] btrfs-progs: check fopen failure in cmds-send

Check for fopen() failure.  This shows up in static analysis as a
possible null pointer derference.

Signed-off-by: Zach Brown <zab@redhat.com>
Laughed-at-by: Stefan Behrens <sbehrens@giantdisaster.de>
---
 cmds-send.c | 3 +++
 1 file changed, 3 insertions(+)
diff mbox

Patch

diff --git a/cmds-send.c b/cmds-send.c
index 374d040..81b3e49 100644
--- a/cmds-send.c
+++ b/cmds-send.c
@@ -72,6 +72,9 @@  int find_mount_root(const char *path, char **mount_root)
 	close(fd);
 
 	mnttab = fopen("/proc/mounts", "r");
+	if (!mnttab)
+		return -errno;
+
 	while ((ent = getmntent(mnttab))) {
 		len = strlen(ent->mnt_dir);
 		if (strncmp(ent->mnt_dir, path, len) == 0) {