X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=builtin-apply.c;h=9641a6479a5325d5d1680d5e313a23e5d536f822;hb=24ff4d56cf400126aa93ac9a5b9d8a21afadf3f6;hp=541493e1ba81462f4b01bb99c2840938bae02e4f;hpb=d0605072911a4c93a7eacf1fb55e79227c457e34;p=git.git diff --git a/builtin-apply.c b/builtin-apply.c index 541493e1b..9641a6479 100644 --- a/builtin-apply.c +++ b/builtin-apply.c @@ -404,6 +404,9 @@ static char *squash_slash(char *name) { int i = 0, j = 0; + if (!name) + return NULL; + while (name[i]) { if ((name[j++] = name[i++]) == '/') while (name[i] == '/') @@ -416,7 +419,10 @@ static char *squash_slash(char *name) static char *find_name(const char *line, char *def, int p_value, int terminate) { int len; - const char *start = line; + const char *start = NULL; + + if (p_value == 0) + start = line; if (*line == '"') { struct strbuf name = STRBUF_INIT; @@ -686,7 +692,7 @@ static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name, if (isnull) die("git apply: bad git-diff - expected /dev/null, got %s on line %d", name, linenr); another = find_name(line, NULL, p_value, TERM_TAB); - if (!another || memcmp(another, name, len)) + if (!another || memcmp(another, name, len + 1)) die("git apply: bad git-diff - inconsistent %s filename on line %d", oldnew, linenr); free(another); return orig_name; @@ -1199,7 +1205,8 @@ static int find_header(char *line, unsigned long size, int *hdrsize, struct patc continue; if (!patch->old_name && !patch->new_name) { if (!patch->def_name) - die("git diff header lacks filename information (line %d)", linenr); + die("git diff header lacks filename information when removing " + "%d leading pathname components (line %d)" , p_value, linenr); patch->old_name = patch->new_name = patch->def_name; } patch->is_toplevel_relative = 1; @@ -1898,20 +1905,15 @@ static int match_fragment(struct image *img, } /* - * Ok, the preimage matches with whitespace fuzz. Update it and - * the common postimage lines to use the same whitespace as the - * target. imgoff now holds the true length of the target that - * matches the preimage, and we need to update the line lengths - * of the preimage to match the target ones. + * Ok, the preimage matches with whitespace fuzz. + * + * imgoff now holds the true length of the target that + * matches the preimage. Update the preimage and + * the common postimage context lines to use the same + * whitespace as the target. */ fixed_buf = xmalloc(imgoff); memcpy(fixed_buf, img->buf + try, imgoff); - for (i = 0; i < preimage->nr; i++) - preimage->line[i].len = img->line[try_lno+i].len; - - /* - * Update the preimage buffer and the postimage context lines. - */ update_pre_post_images(preimage, postimage, fixed_buf, imgoff, postlen); return 1; @@ -1995,11 +1997,8 @@ static int find_pos(struct image *img, unsigned long backwards, forwards, try; int backwards_lno, forwards_lno, try_lno; - if (preimage->nr > img->nr) - return -1; - /* - * If match_begining or match_end is specified, there is no + * If match_beginning or match_end is specified, there is no * point starting from a wrong line that will never match and * wander around and wait for a match at the specified end. */ @@ -2008,7 +2007,12 @@ static int find_pos(struct image *img, else if (match_end) line = img->nr - preimage->nr; - if (line > img->nr) + /* + * Because the comparison is unsigned, the following test + * will also take care of a negative line number that can + * result when match_end and preimage is larger than the target. + */ + if ((size_t) line > img->nr) line = img->nr; try = 0;