[PATCH 1/1] cat-file: fix a memory leak in cat_one_file

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Sometimes we should free output buffer in cat_one_file, but not always.
It must to be freed only if it was allocated during git cat-file -t,
commit,tag and tree types handling.

Signed-off-by: Alexander Kuleshov <kuleshovmail@xxxxxxxxx>
---
 builtin/cat-file.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/builtin/cat-file.c b/builtin/cat-file.c
index f8d8129..f5dad60 100644
--- a/builtin/cat-file.c
+++ b/builtin/cat-file.c
@@ -18,6 +18,7 @@ static int cat_one_file(int opt, const char
*exp_type, const char *obj_name)
     unsigned char sha1[20];
     enum object_type type;
     char *buf;
+    int buf_must_free = 0;
     unsigned long size;
     struct object_context obj_context;

@@ -68,9 +69,14 @@ static int cat_one_file(int opt, const char
*exp_type, const char *obj_name)

         if (type == OBJ_BLOB)
             return stream_blob_to_fd(1, sha1, NULL, 0);
+
         buf = read_sha1_file(sha1, &type, &size);
-        if (!buf)
+        buf_must_free = 1;
+
+        if (!buf) {
+            free(buf);
             die("Cannot read object %s", obj_name);
+        }

         /* otherwise just spit out the data */
         break;
@@ -100,6 +106,7 @@ static int cat_one_file(int opt, const char
*exp_type, const char *obj_name)
              */
         }
         buf = read_object_with_reference(sha1, exp_type, &size, NULL);
+        buf_must_free = 1;
         break;

     default:
@@ -110,6 +117,10 @@ static int cat_one_file(int opt, const char
*exp_type, const char *obj_name)
         die("git cat-file %s: bad file", obj_name);

     write_or_die(1, buf, size);
+
+    if (buf_must_free)
+        free(buf);
+
     return 0;
 }

-- 
2.2.0.rc3.224.gb2e243b
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]