Skip to contentSkip to conversation

xfsdump: Fix memory leak in invutil/stobj.c

PATCH Superseded Boris Ranto 2 participants 2 messages Mar 5, 2014 – Mar 6, 2014

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xfsdump: Fix memory leak in invutil/stobj.c

The function open_stobj duplicates its argument, upon successful duplication, the fstat is called. If the fstat command fails then the memory for the duplicated string is leaked. Fix this by moving the string duplication after the fstat call. This is ok because the fstat call does not use the duplicated string.

---
 invutil/stobj.c |   12 ++++++------
 1 files changed, 6 insertions(+), 6 deletions(-)
invutil/stobj.c +6 -6
index 428b419..55abe63 100644
@@ -578,12 +578,6 @@ open_stobj(char *StObjFileName)
 	return fd;
     }
 
-    name = strdup(StObjFileName);
-    if(name == NULL) {
-	fprintf(stderr, "%s: internal memory error: strdup stobj_name\n", g_programName);
-	exit(1);
-    }
-
     read_n_bytes(fd, &cnt, sizeof(invt_sescounter_t), StObjFileName);
     lseek( fd, 0, SEEK_SET );
     errno = 0;
@@ -595,6 +589,12 @@ open_stobj(char *StObjFileName)
     size = sb.st_size;
     mapaddr = mmap_n_bytes(fd, size, BOOL_FALSE, StObjFileName);
 
+    name = strdup(StObjFileName);
+    if(name == NULL) {
+	fprintf(stderr, "%s: internal memory error: strdup stobj_name\n", g_programName);
+	exit(1);
+    }
+
     return add_stobj(name, fd, size, mapaddr, (invt_sescounter_t *)mapaddr);
 }
 
Signature · 6 lines
1.7.1

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs
#1 lore raw
Quoted 8 lines · Boris Ranto

The function open_stobj duplicates its argument, upon successful duplication, the fstat is called. If the fstat command fails then the memory for the duplicated string is leaked. Fix this by moving the string duplication after the fstat call. This is ok because the fstat call does not use the duplicated string.

---

The fix looks fine to me, but in the same scenario it looks like we also leak the fd. Care to clean that up in this patch as well? :)

Brian

Quoted 40 lines
 invutil/stobj.c |   12 ++++++------
 1 files changed, 6 insertions(+), 6 deletions(-)
invutil/stobj.c +6 -6
index 428b419..55abe63 100644
@@ -578,12 +578,6 @@ open_stobj(char *StObjFileName)
 	return fd;
     }
 
-    name = strdup(StObjFileName);
-    if(name == NULL) {
-	fprintf(stderr, "%s: internal memory error: strdup stobj_name\n", g_programName);
-	exit(1);
-    }
-
     read_n_bytes(fd, &cnt, sizeof(invt_sescounter_t), StObjFileName);
     lseek( fd, 0, SEEK_SET );
     errno = 0;
@@ -595,6 +589,12 @@ open_stobj(char *StObjFileName)
     size = sb.st_size;
     mapaddr = mmap_n_bytes(fd, size, BOOL_FALSE, StObjFileName);
 
+    name = strdup(StObjFileName);
+    if(name == NULL) {
+	fprintf(stderr, "%s: internal memory error: strdup stobj_name\n", g_programName);
+	exit(1);
+    }
+
     return add_stobj(name, fd, size, mapaddr, (invt_sescounter_t *)mapaddr);
 }
 
Signature · 6 lines
1.7.1

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs

_______________________________________________
xfs mailing list
[email protected]
http://oss.sgi.com/mailman/listinfo/xfs