@@ -236,6 +236,7 @@ static struct commit_graft *lookup_commit_graft(const unsigned char *sha1)
236236
237237int parse_commit_buffer (struct commit * item , void * buffer , unsigned long size )
238238{
239+ char * tail = buffer ;
239240 char * bufptr = buffer ;
240241 unsigned char parent [20 ];
241242 struct commit_list * * pptr ;
@@ -245,9 +246,10 @@ int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
245246 if (item -> object .parsed )
246247 return 0 ;
247248 item -> object .parsed = 1 ;
248- if (memcmp (bufptr , "tree " , 5 ))
249+ tail += size ;
250+ if (tail <= bufptr + 5 || memcmp (bufptr , "tree " , 5 ))
249251 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 )
251253 return error ("bad tree pointer in commit %s" ,
252254 sha1_to_hex (item -> object .sha1 ));
253255 item -> tree = lookup_tree (parent );
@@ -257,10 +259,12 @@ int parse_commit_buffer(struct commit *item, void *buffer, unsigned long size)
257259 pptr = & item -> parents ;
258260
259261 graft = lookup_commit_graft (item -> object .sha1 );
260- while (!memcmp (bufptr , "parent " , 7 )) {
262+ while (bufptr + 48 < tail && !memcmp (bufptr , "parent " , 7 )) {
261263 struct commit * new_parent ;
262264
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' )
264268 return error ("bad parents in commit %s" , sha1_to_hex (item -> object .sha1 ));
265269 bufptr += 48 ;
266270 if (graft )
0 commit comments