Skip to content

Commit 365889e

Browse files
committed
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]>
1 parent 4a4e0aa commit 365889e

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
@@ -3076,6 +3076,15 @@ char *git_url_basename(const char *repo, int is_bundle, int is_bare)
30763076
end--;
30773077
}
30783078

3079+
/*
3080+
* It should not be possible to overflow `ptrdiff_t` by passing in an
3081+
* insanely long URL, but GCC does not know that and will complain
3082+
* without this check.
3083+
*/
3084+
if (end - start < 0)
3085+
die(_("No directory name could be guessed.\n"
3086+
"Please specify a directory on the command line"));
3087+
30793088
/*
30803089
* Strip trailing port number if we've got only a
30813090
* hostname (that is, there is no dir separator but a

0 commit comments

Comments
 (0)