From: Junio C Hamano Date: Mon, 11 Feb 2008 23:48:27 GMT Subject: Re: [PATCH] apply: fix segfault Message-ID: <7vejbj6oms.fsf@gitster.siamese.dyndns.org> In-Reply-To: <7vprv36qkt.fsf@gitster.siamese.dyndns.org> Junio C Hamano writes: > Johannes Schindelin writes: > >> When the patch reports a line number that is larger than the number of >> lines in the current version of the file, git-apply used to segfault. > > I have to wonder if the correct fix should be like this > instead. Under that condition, I think computation of the > initial "try" value already oversteps the line[] array for the > original image. > > diff --git a/builtin-apply.c b/builtin-apply.c > index 2b8ba81..177f541 100644 > --- a/builtin-apply.c > +++ b/builtin-apply.c > @@ -1809,6 +1809,9 @@ static int find_pos(struct image *img, > else if (match_end) > line = img->nr - preimage->nr; > > + if (line > preimage->nr) > + line = preimage->nr; > + > try = 0; > for (i = 0; i < line; i++) > try += img->line[i].len; Sorry, obviously the check should be against img->nr not the preimage.