xfs
[Top] [All Lists]

[PATCH v2] xfsdump: Fix memory and fd leak in invutil/stobj.c

To: xfs@xxxxxxxxxxx
Subject: [PATCH v2] xfsdump: Fix memory and fd leak in invutil/stobj.c
From: Boris Ranto <ranto.boris@xxxxxxxxx>
Date: Fri, 7 Mar 2014 15:52:42 +0100
Cc: branto@xxxxxxxxxx, Boris Ranto <ranto.boris@xxxxxxxxx>
Delivered-to: xfs@xxxxxxxxxxx
Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id; bh=IgPV+uIm/fj87FCGodLrzsJ9LORJE/Dl2PCYA2n0gAU=; b=A8K/5zqlDLRhsDnRliowpEYrVkZZdNxakPVh7crrT9wtIXZUGD6diz2rRZsa/YBGcq 19vXDFXPU/bFaISAglJxC5puHE2WVUuFXvuRg+xI/y491w39zIzigFPBva817h3rCrVd f8Hi86LRIuAR1vb3TJxPHvF9cbCIFfFhQNkiRWKOSV/0rv8ccOA13pYzPbtElosHya5l 2+VveX+TPrr3EXTxqZdGsxcr/6On/6/Im8b9hhe5isSbxAHbIFSC3kQiC3GTplY0GhR+ /mFya3/PZbxCWQrODtQyNlCmcPIFPCDBKCoMK5H1RQ8f/QzvqCwQDpK3KjYaQrYCCs/O peCg==
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.

Brian Foster noticed that the function also leaks a file descriptor
in case the file cannot be fstated. Fixing that, too.

Signed-off-by: Boris Ranto <ranto.boris@xxxxxxxxx>
---
 invutil/stobj.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/invutil/stobj.c b/invutil/stobj.c
index 428b419..a74ba0f 100644
--- a/invutil/stobj.c
+++ b/invutil/stobj.c
@@ -578,23 +578,24 @@ 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;
     if (fstat(fd, &sb) < 0) {
        fprintf(stderr, "Could not get stat info on %s\n", StObjFileName);
        perror("fstat");
+       close(fd);
        return -1;
     }
     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);
 }
 
-- 
1.9.0

<Prev in Thread] Current Thread [Next in Thread>