{"thread":{"id":"476","subject":"[PATCH] Fix memory leaks in git-fsck-cache","startedAt":"2005-05-04T17:44:15Z","lastAt":"2005-05-04T17:44:15Z","messageCount":1,"participants":["Sergey Vlasov"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"2583","messageId":"20050504174415.GC29536@master.mivlgu.local","threadId":"476","inReplyTo":null,"subject":"[PATCH] Fix memory leaks in git-fsck-cache","fromName":"Sergey Vlasov","fromEmail":"vsu@altlinux.ru","sentAt":"2005-05-04T17:44:15Z","receivedAt":"2005-05-04T17:44:15Z","isPatch":true,"sender":{"key":"vsu@altlinux.ru","avatar":"https://avatars.githubusercontent.com/u/616082?v=4"},"body":"This patch fixes memory leaks in parse_object() and related functions;\nthese leaks were very noticeable when running git-fsck-cache.\n\nSigned-off-by: Sergey Vlasov <vsu@altlinux.ru>\n\n---\n\n blob.c   |    1 +\n commit.c |    4 +++-\n object.c |    3 ++-\n tag.c    |   21 ++++++++++++++-------\n tree.c   |    9 +++++++--\n 5 files changed, 27 insertions(+), 11 deletions(-)\n\nIndex: blob.c\n===================================================================\n--- 51a882a2dc62e0d3cdc79e0badc61559fb723481/blob.c  (mode:100644 sha1:d4af4a309433744d2fe819886d66741ab016f62b)\n+++ uncommitted/blob.c  (mode:100644)\n@@ -34,6 +34,7 @@ int parse_blob(struct blob *item)\n         if (!buffer)\n                 return error(\"Could not read %s\",\n                              sha1_to_hex(item->object.sha1));\n+\tfree(buffer);\n         if (strcmp(type, blob_type))\n                 return error(\"Object %s not a blob\",\n                              sha1_to_hex(item->object.sha1));\nIndex: commit.c\n===================================================================\n--- 51a882a2dc62e0d3cdc79e0badc61559fb723481/commit.c  (mode:100644 sha1:3956c7ba961781f72b39c42368df1e76b2d035dd)\n+++ uncommitted/commit.c  (mode:100644)\n@@ -54,9 +54,11 @@ int parse_commit(struct commit *item)\n \tif (!buffer)\n \t\treturn error(\"Could not read %s\",\n \t\t\t     sha1_to_hex(item->object.sha1));\n-\tif (strcmp(type, commit_type))\n+\tif (strcmp(type, commit_type)) {\n+\t\tfree(buffer);\n \t\treturn error(\"Object %s not a commit\",\n \t\t\t     sha1_to_hex(item->object.sha1));\n+\t}\n \tget_sha1_hex(bufptr + 5, parent);\n \titem->tree = lookup_tree(parent);\n \tif (item->tree)\nIndex: object.c\n===================================================================\n--- 51a882a2dc62e0d3cdc79e0badc61559fb723481/object.c  (mode:100644 sha1:ca4af8fa2dc0672b92310a3ebdd4d14bf070dd69)\n+++ uncommitted/object.c  (mode:100644)\n@@ -107,11 +107,12 @@ struct object *parse_object(unsigned cha\n \t\tchar type[100];\n \t\tunsigned long size;\n \t\tvoid *buffer = unpack_sha1_file(map, mapsize, type, &size);\n+\t\tmunmap(map, mapsize);\n \t\tif (!buffer)\n \t\t\treturn NULL;\n \t\tif (check_sha1_signature(sha1, buffer, size, type) < 0)\n \t\t\tprintf(\"sha1 mismatch %s\\n\", sha1_to_hex(sha1));\n-\t\tmunmap(map, mapsize);\n+\t\tfree(buffer);\n \t\tif (!strcmp(type, \"blob\")) {\n \t\t\tstruct blob *ret = lookup_blob(sha1);\n \t\t\tparse_blob(ret);\nIndex: tag.c\n===================================================================\n--- 51a882a2dc62e0d3cdc79e0badc61559fb723481/tag.c  (mode:100644 sha1:406dba2aa5607332fe022fcba1beb045fa61c5f4)\n+++ uncommitted/tag.c  (mode:100644)\n@@ -37,37 +37,44 @@ int parse_tag(struct tag *item)\n         if (!data)\n                 return error(\"Could not read %s\",\n                              sha1_to_hex(item->object.sha1));\n-        if (strcmp(type, tag_type))\n+        if (strcmp(type, tag_type)) {\n+\t\tfree(data);\n                 return error(\"Object %s not a tag\",\n                              sha1_to_hex(item->object.sha1));\n+\t}\n \n \tif (size < 64)\n-\t\treturn -1;\n+\t\tgoto err;\n \tif (memcmp(\"object \", data, 7) || get_sha1_hex(data + 7, object))\n-\t\treturn -1;\n+\t\tgoto err;\n \n \titem->tagged = parse_object(object);\n \n \ttype_line = data + 48;\n \tif (memcmp(\"\\ntype \", type_line-1, 6))\n-\t\treturn -1;\n+\t\tgoto err;\n \n \ttag_line = strchr(type_line, '\\n');\n \tif (!tag_line || memcmp(\"tag \", ++tag_line, 4))\n-\t\treturn -1;\n+\t\tgoto err;\n \n \tsig_line = strchr(tag_line, '\\n');\n \tif (!sig_line)\n-\t\treturn -1;\n+\t\tgoto err;\n \tsig_line++;\n \n \ttypelen = tag_line - type_line - strlen(\"type \\n\");\n \tif (typelen >= 20)\n-\t\treturn -1;\n+\t\tgoto err;\n \ttaglen = sig_line - tag_line - strlen(\"tag \\n\");\n \titem->tag = xmalloc(taglen + 1);\n \tmemcpy(item->tag, tag_line + 4, taglen);\n \titem->tag[taglen] = '\\0';\n \n+\tfree(data);\n \treturn 0;\n+\n+err:\n+\tfree(data);\n+\treturn -1;\n }\nIndex: tree.c\n===================================================================\n--- 51a882a2dc62e0d3cdc79e0badc61559fb723481/tree.c  (mode:100644 sha1:72305a357b6694cdb1b29dad9975902fdef86457)\n+++ uncommitted/tree.c  (mode:100644)\n@@ -101,9 +101,11 @@ int parse_tree(struct tree *item)\n \tif (!buffer)\n \t\treturn error(\"Could not read %s\",\n \t\t\t     sha1_to_hex(item->object.sha1));\n-\tif (strcmp(type, tree_type))\n+\tif (strcmp(type, tree_type)) {\n+\t\tfree(buffer);\n \t\treturn error(\"Object %s not a tree\",\n \t\t\t     sha1_to_hex(item->object.sha1));\n+\t}\n \tlist_p = &item->entries;\n \twhile (size) {\n \t\tstruct object *obj;\n@@ -113,8 +115,10 @@ int parse_tree(struct tree *item)\n \t\tchar *path = strchr(bufptr, ' ');\n \t\tunsigned int mode;\n \t\tif (size < len + 20 || !path || \n-\t\t    sscanf(bufptr, \"%o\", &mode) != 1)\n+\t\t    sscanf(bufptr, \"%o\", &mode) != 1) {\n+\t\t\tfree(buffer);\n \t\t\treturn -1;\n+\t\t}\n \n \t\tentry = xmalloc(sizeof(struct tree_entry_list));\n \t\tentry->name = strdup(path + 1);\n@@ -138,5 +142,6 @@ int parse_tree(struct tree *item)\n \t\t*list_p = entry;\n \t\tlist_p = &entry->next;\n \t}\n+\tfree(buffer);\n \treturn 0;\n }\n"}]}