xfs
[Top] [All Lists]

[PATCH v2 1/4] xfsprogs: xfs_io: fix a memory leak in imap_f

To: xfs@xxxxxxxxxxx
Subject: [PATCH v2 1/4] xfsprogs: xfs_io: fix a memory leak in imap_f
From: Vivek Trivedi <t.vivek@xxxxxxxxxxx>
Date: Fri, 11 Dec 2015 16:39:00 +0530
Cc: a.sahrawat@xxxxxxxxxxx, pankaj.m@xxxxxxxxxxx, Vivek Trivedi <t.vivek@xxxxxxxxxxx>
Delivered-to: xfs@xxxxxxxxxxx
Dlp-filter: Pass
In-reply-to: <1449832143-26356-1-git-send-email-t.vivek@xxxxxxxxxxx>
References: <1449832143-26356-1-git-send-email-t.vivek@xxxxxxxxxxx>
add NULL check for malloc return and free allocated memory in
return path in imap_f

Signed-off-by: Vivek Trivedi <t.vivek@xxxxxxxxxxx>
---
 io/imap.c |    7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/io/imap.c b/io/imap.c
index 34901cb..6ed98eb 100644
--- a/io/imap.c
+++ b/io/imap.c
@@ -39,6 +39,8 @@ imap_f(int argc, char **argv)
                nent = atoi(argv[1]);
 
        t = malloc(nent * sizeof(*t));
+       if (!t)
+               return 0;
 
        bulkreq.lastip  = &last;
        bulkreq.icount  = nent;
@@ -46,8 +48,10 @@ imap_f(int argc, char **argv)
        bulkreq.ocount  = &count;
 
        while (xfsctl(file->name, file->fd, XFS_IOC_FSINUMBERS, &bulkreq) == 0) 
{
-               if (count == 0)
+               if (count == 0) {
+                       free(t);
                        return 0;
+               }
                for (i = 0; i < count; i++) {
                        printf(_("ino %10llu count %2d mask %016llx\n"),
                                (unsigned long long)t[i].xi_startino,
@@ -55,6 +59,7 @@ imap_f(int argc, char **argv)
                                (unsigned long long)t[i].xi_allocmask);
                }
        }
+       free(t);
        perror("xfsctl(XFS_IOC_FSINUMBERS)");
        exitcode = 1;
        return 0;
-- 
1.7.9.5

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