Skip to content

Commit 13019d4

Browse files
sigprofLinus Torvalds
authored andcommitted
[PATCH] Fix memory leaks in git-fsck-cache
This patch fixes memory leaks in parse_object() and related functions; these leaks were very noticeable when running git-fsck-cache. Signed-off-by: Sergey Vlasov <vsu@altlinux.ru> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
1 parent 705acc5 commit 13019d4

File tree

5 files changed

+27
-11
lines changed

5 files changed

+27
-11
lines changed

blob.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ int parse_blob(struct blob *item)
3434
if (!buffer)
3535
return error("Could not read %s",
3636
sha1_to_hex(item->object.sha1));
37+
free(buffer);
3738
if (strcmp(type, blob_type))
3839
return error("Object %s not a blob",
3940
sha1_to_hex(item->object.sha1));

commit.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,11 @@ int parse_commit(struct commit *item)
5454
if (!buffer)
5555
return error("Could not read %s",
5656
sha1_to_hex(item->object.sha1));
57-
if (strcmp(type, commit_type))
57+
if (strcmp(type, commit_type)) {
58+
free(buffer);
5859
return error("Object %s not a commit",
5960
sha1_to_hex(item->object.sha1));
61+
}
6062
get_sha1_hex(bufptr + 5, parent);
6163
item->tree = lookup_tree(parent);
6264
if (item->tree)

object.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,12 @@ struct object *parse_object(unsigned char *sha1)
107107
char type[100];
108108
unsigned long size;
109109
void *buffer = unpack_sha1_file(map, mapsize, type, &size);
110+
munmap(map, mapsize);
110111
if (!buffer)
111112
return NULL;
112113
if (check_sha1_signature(sha1, buffer, size, type) < 0)
113114
printf("sha1 mismatch %s\n", sha1_to_hex(sha1));
114-
munmap(map, mapsize);
115+
free(buffer);
115116
if (!strcmp(type, "blob")) {
116117
struct blob *ret = lookup_blob(sha1);
117118
parse_blob(ret);

tag.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,37 +37,44 @@ int parse_tag(struct tag *item)
3737
if (!data)
3838
return error("Could not read %s",
3939
sha1_to_hex(item->object.sha1));
40-
if (strcmp(type, tag_type))
40+
if (strcmp(type, tag_type)) {
41+
free(data);
4142
return error("Object %s not a tag",
4243
sha1_to_hex(item->object.sha1));
44+
}
4345

4446
if (size < 64)
45-
return -1;
47+
goto err;
4648
if (memcmp("object ", data, 7) || get_sha1_hex(data + 7, object))
47-
return -1;
49+
goto err;
4850

4951
item->tagged = parse_object(object);
5052

5153
type_line = data + 48;
5254
if (memcmp("\ntype ", type_line-1, 6))
53-
return -1;
55+
goto err;
5456

5557
tag_line = strchr(type_line, '\n');
5658
if (!tag_line || memcmp("tag ", ++tag_line, 4))
57-
return -1;
59+
goto err;
5860

5961
sig_line = strchr(tag_line, '\n');
6062
if (!sig_line)
61-
return -1;
63+
goto err;
6264
sig_line++;
6365

6466
typelen = tag_line - type_line - strlen("type \n");
6567
if (typelen >= 20)
66-
return -1;
68+
goto err;
6769
taglen = sig_line - tag_line - strlen("tag \n");
6870
item->tag = xmalloc(taglen + 1);
6971
memcpy(item->tag, tag_line + 4, taglen);
7072
item->tag[taglen] = '\0';
7173

74+
free(data);
7275
return 0;
76+
77+
err:
78+
free(data);
79+
return -1;
7380
}

tree.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,11 @@ int parse_tree(struct tree *item)
101101
if (!buffer)
102102
return error("Could not read %s",
103103
sha1_to_hex(item->object.sha1));
104-
if (strcmp(type, tree_type))
104+
if (strcmp(type, tree_type)) {
105+
free(buffer);
105106
return error("Object %s not a tree",
106107
sha1_to_hex(item->object.sha1));
108+
}
107109
list_p = &item->entries;
108110
while (size) {
109111
struct object *obj;
@@ -113,8 +115,10 @@ int parse_tree(struct tree *item)
113115
char *path = strchr(bufptr, ' ');
114116
unsigned int mode;
115117
if (size < len + 20 || !path ||
116-
sscanf(bufptr, "%o", &mode) != 1)
118+
sscanf(bufptr, "%o", &mode) != 1) {
119+
free(buffer);
117120
return -1;
121+
}
118122

119123
entry = xmalloc(sizeof(struct tree_entry_list));
120124
entry->name = strdup(path + 1);
@@ -138,5 +142,6 @@ int parse_tree(struct tree *item)
138142
*list_p = entry;
139143
list_p = &entry->next;
140144
}
145+
free(buffer);
141146
return 0;
142147
}

0 commit comments

Comments
 (0)