[PATCH] Fix memory leaks in git-fsck-cache
[git.git] / tag.c
1 #include "tag.h"
2 #include "cache.h"
3
4 const char *tag_type = "tag";
5
6 struct tag *lookup_tag(unsigned char *sha1)
7 {
8         struct object *obj = lookup_object(sha1);
9         if (!obj) {
10                 struct tag *ret = xmalloc(sizeof(struct tag));
11                 memset(ret, 0, sizeof(struct tag));
12                 created_object(sha1, &ret->object);
13                 ret->object.type = tag_type;
14                 return ret;
15         }
16         if (obj->type != tag_type) {
17                 error("Object %s is a %s, not a tree", 
18                       sha1_to_hex(sha1), obj->type);
19                 return NULL;
20         }
21         return (struct tag *) obj;
22 }
23
24 int parse_tag(struct tag *item)
25 {
26         char type[20];
27         void *data, *bufptr;
28         unsigned long size;
29         int typelen, taglen;
30         unsigned char object[20];
31         const char *type_line, *tag_line, *sig_line;
32
33         if (item->object.parsed)
34                 return 0;
35         item->object.parsed = 1;
36         data = bufptr = read_sha1_file(item->object.sha1, type, &size);
37         if (!data)
38                 return error("Could not read %s",
39                              sha1_to_hex(item->object.sha1));
40         if (strcmp(type, tag_type)) {
41                 free(data);
42                 return error("Object %s not a tag",
43                              sha1_to_hex(item->object.sha1));
44         }
45
46         if (size < 64)
47                 goto err;
48         if (memcmp("object ", data, 7) || get_sha1_hex(data + 7, object))
49                 goto err;
50
51         item->tagged = parse_object(object);
52
53         type_line = data + 48;
54         if (memcmp("\ntype ", type_line-1, 6))
55                 goto err;
56
57         tag_line = strchr(type_line, '\n');
58         if (!tag_line || memcmp("tag ", ++tag_line, 4))
59                 goto err;
60
61         sig_line = strchr(tag_line, '\n');
62         if (!sig_line)
63                 goto err;
64         sig_line++;
65
66         typelen = tag_line - type_line - strlen("type \n");
67         if (typelen >= 20)
68                 goto err;
69         taglen = sig_line - tag_line - strlen("tag \n");
70         item->tag = xmalloc(taglen + 1);
71         memcpy(item->tag, tag_line + 4, taglen);
72         item->tag[taglen] = '\0';
73
74         free(data);
75         return 0;
76
77 err:
78         free(data);
79         return -1;
80 }