Skip to content

Commit c6f9ad7

Browse files
committed
patch_parse: fix undefined behaviour due to arithmetic on NULL pointers
Doing arithmetic with NULL pointers is undefined behaviour in the C standard. We do so regardless when parsing patches, as we happily add a potential prefix length to prefixed paths. While this works out just fine as the prefix length is always equal to zero in these cases, thus resulting in another NULL pointer, it still is undefined behaviour and was pointed out to us by OSSfuzz. Fix the issue by checking whether paths are NULL, avoiding the arithmetic if they are.
1 parent 3e6a904 commit c6f9ad7

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/patch_parse.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,13 +1025,17 @@ static int check_filenames(git_patch_parsed *patch)
10251025
/* Prefer the rename filenames as they are unambiguous and unprefixed */
10261026
if (patch->rename_old_path)
10271027
patch->base.delta->old_file.path = patch->rename_old_path;
1028-
else
1028+
else if (prefixed_old)
10291029
patch->base.delta->old_file.path = prefixed_old + old_prefixlen;
1030+
else
1031+
patch->base.delta->old_file.path = NULL;
10301032

10311033
if (patch->rename_new_path)
10321034
patch->base.delta->new_file.path = patch->rename_new_path;
1033-
else
1035+
else if (prefixed_new)
10341036
patch->base.delta->new_file.path = prefixed_new + new_prefixlen;
1037+
else
1038+
patch->base.delta->new_file.path = NULL;
10351039

10361040
if (!patch->base.delta->old_file.path &&
10371041
!patch->base.delta->new_file.path)

0 commit comments

Comments
 (0)