Skip to content

Commit b349734

Browse files
committed
patch_parse: fix parsing addition/deletion of file with space
The diff header format is a strange beast in that it is inherently unparseable in an unambiguous way. While parsing a/file.txt b/file.txt is obvious and trivially doable, parsing a diff header of a/file b/file ab.txt b/file b/file ab.txt is not (but in fact valid and created by git.git). Due to that, we have relaxed our diff header parser in commit 80226b5 (patch_parse: allow parsing ambiguous patch headers, 2017-09-22), so that we started to bail out when seeing diff headers with spaces in their file names. Instead, we try to use the "---" and "+++" lines, which are unambiguous. In some cases, though, we neither have a useable file name from the header nor from the "---" or "+++" lines. This is the case when we have a deletion or addition of a file with spaces: the header is unparseable and the other lines will simply show "/dev/null". This trips our parsing logic when we try to extract the prefix (the "a/" part) that is being used in the path line, where we unconditionally try to dereference a NULL pointer in such a scenario. We can fix this by simply not trying to parse the prefix in cases where we have no useable path name. That'd leave the parsed patch without either `old_prefix` or `new_prefix` populated. But in fact such cases are already handled by users of the patch object, which simply opt to use the default prefixes in that case.
1 parent 131cd9b commit b349734

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/patch_parse.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -928,8 +928,8 @@ static int check_filenames(git_patch_parsed *patch)
928928
prefixed_old = (!added && patch->old_path) ? patch->old_path : patch->header_old_path;
929929
prefixed_new = (!deleted && patch->new_path) ? patch->new_path : patch->header_new_path;
930930

931-
if (check_prefix(&patch->old_prefix, &old_prefixlen, patch, prefixed_old) < 0 ||
932-
check_prefix(&patch->new_prefix, &new_prefixlen, patch, prefixed_new) < 0)
931+
if ((prefixed_old && check_prefix(&patch->old_prefix, &old_prefixlen, patch, prefixed_old) < 0) ||
932+
(prefixed_new && check_prefix(&patch->new_prefix, &new_prefixlen, patch, prefixed_new) < 0))
933933
return -1;
934934

935935
/* Prefer the rename filenames as they are unambiguous and unprefixed */

0 commit comments

Comments
 (0)