Skip to content

Commit a6e063c

Browse files
dscho1480c1
authored andcommitted
dir.c: avoid "exceeds maximum object size" error with GCC v12.x
Technically, the pointer difference `end - start` _could_ be negative, and when cast to an (unsigned) `size_t` that would cause problems. In this instance, the symptom is: dir.c: In function 'git_url_basename': dir.c:3087:13: error: 'memchr' specified bound [9223372036854775808, 0] exceeds maximum object size 9223372036854775807 [-Werror=stringop-overread] CC ewah/bitmap.o 3087 | if (memchr(start, '/', end - start) == NULL | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ While it is a bit far-fetched to think that `end` (which is defined as `repo + strlen(repo)`) and `start` (which starts at `repo` and never steps beyond the NUL terminator) could result in such a negative difference, GCC has no way of knowing that. See also https://gcc.gnu.org/bugzilla//show_bug.cgi?id=85783. Let's just add a safety check, primarily for GCC's benefit. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d8aaafe commit a6e063c

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

dir.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3124,6 +3124,15 @@ char *git_url_basename(const char *repo, int is_bundle, int is_bare)
31243124
end--;
31253125
}
31263126

3127+
/*
3128+
* It should not be possible to overflow `ptrdiff_t` by passing in an
3129+
* insanely long URL, but GCC does not know that and will complain
3130+
* without this check.
3131+
*/
3132+
if (end - start < 0)
3133+
die(_("No directory name could be guessed.\n"
3134+
"Please specify a directory on the command line"));
3135+
31273136
/*
31283137
* Strip trailing port number if we've got only a
31293138
* hostname (that is, there is no dir separator but a

0 commit comments

Comments
 (0)