connect.c: check the commit buffer boundary while parsing.
Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Jun 28, 2006 at 03:51 UTC
3b44f15a35b3550f34a4f796706fceccdc90d506
1 file changed
+8
-4
commit.c
+8
-4
@@ -236,6 +236,7 @@ static struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
236
237
int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
238
{
239
+ char *tail = buffer;
240
char *bufptr = buffer;
241
unsigned char parent[20];
242
struct commit_list **pptr;
@@ -245,9 +246,10 @@ int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
246
if (item->object.parsed)
247
return 0;
248
item->object.parsed = 1;
248
- if (memcmp(bufptr, "tree ", 5))
249
+ tail += size;
250
+ if (tail <= bufptr + 5 || memcmp(bufptr, "tree ", 5))
251
return error("bogus commit object %s", sha1_to_hex(item->object.sha1));
250
- if (get_sha1_hex(bufptr + 5, parent) < 0)
252
+ if (tail <= bufptr + 45 || get_sha1_hex(bufptr + 5, parent) < 0)
253
return error("bad tree pointer in commit %s",
254
sha1_to_hex(item->object.sha1));
255
item->tree = lookup_tree(parent);
@@ -257,10 +259,12 @@ int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
259
pptr = &item->parents;
260
261
graft = lookup_commit_graft(item->object.sha1);
260
- while (!memcmp(bufptr, "parent ", 7)) {
262
+ while (bufptr + 48 < tail && !memcmp(bufptr, "parent ", 7)) {
263
struct commit *new_parent;
264
263
- if (get_sha1_hex(bufptr + 7, parent) || bufptr[47] != '\n')
265
+ if (tail <= bufptr + 48 ||
266
+ get_sha1_hex(bufptr + 7, parent) ||
267
+ bufptr[47] != '\n')
268
return error("bad parents in commit %s", sha1_to_hex(item->object.sha1));
269
bufptr += 48;
270
if (graft)