From 8bc172e5f29894d440aab772ae3a49eb2eaf5585 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Sat, 1 Jul 2017 11:10:07 +0200 Subject: apply: use starts_with() in gitdiff_verify_name() Avoid running over the end of line -- a C string whose length is not known to this function -- by using starts_with() instead of memcmp(3) for checking if it starts with "/dev/null". Also simply include the newline in the string constant to compare against. Drop a comment that just states the obvious. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- apply.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/apply.c b/apply.c index 5e3780d281..4024e70c54 100644 --- a/apply.c +++ b/apply.c @@ -970,8 +970,7 @@ static int gitdiff_verify_name(struct apply_state *state, } free(another); } else { - /* expect "/dev/null" */ - if (memcmp("/dev/null", line, 9) || line[9] != '\n') + if (!starts_with(line, "/dev/null\n")) return error(_("git apply: bad git-diff - expected /dev/null on line %d"), state->linenr); } -- cgit v1.2.1 From 2d105451c0768fc3e9600dec7bca2376f482521e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Sat, 8 Jul 2017 10:58:42 +0200 Subject: apply: use strcmp(3) for comparing strings in gitdiff_verify_name() We don't know the length of the C string "another". It could be shorter than "name", which we compare it to using memchr(3). Call strcmp(3) instead to avoid running over the end of the former, and get rid of a strlen(3) call as a bonus. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano --- apply.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/apply.c b/apply.c index 4024e70c54..58ffe1b764 100644 --- a/apply.c +++ b/apply.c @@ -956,13 +956,12 @@ static int gitdiff_verify_name(struct apply_state *state, } if (*name) { - int len = strlen(*name); char *another; if (isnull) return error(_("git apply: bad git-diff - expected /dev/null, got %s on line %d"), *name, state->linenr); another = find_name(state, line, NULL, state->p_value, TERM_TAB); - if (!another || memcmp(another, *name, len + 1)) { + if (!another || strcmp(another, *name)) { free(another); return error((side == DIFF_NEW_NAME) ? _("git apply: bad git-diff - inconsistent new filename on line %d") : -- cgit v1.2.1