Skip to content

Commit 7109c88

Browse files
committed
sha1_file.c: avoid gcc signed overflow warnings
With the recent gcc, we get: sha1_file.c: In check_packed_git_: sha1_file.c:527: warning: assuming signed overflow does not occur when assuming that (X + c) < X is always false sha1_file.c:527: warning: assuming signed overflow does not occur when assuming that (X + c) < X is always false for a piece of code that tries to make sure that off_t is large enough to hold more than 2^32 offset. The test tried to make sure these do not wrap-around: /* make sure we can deal with large pack offsets */ off_t x = 0x7fffffffUL, y = 0xffffffffUL; if (x > (x + 1) || y > (y + 1)) { but gcc assumes it can do whatever optimization it wants for a signed overflow (undefined behaviour) and warns about this construct. Follow Linus's suggestion to check sizeof(off_t) instead to work around the problem. Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 399f0a8 commit 7109c88

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

sha1_file.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -521,13 +521,15 @@ static int check_packed_git_idx(const char *path, struct packed_git *p)
521521
munmap(idx_map, idx_size);
522522
return error("wrong index v2 file size in %s", path);
523523
}
524-
if (idx_size != min_size) {
525-
/* make sure we can deal with large pack offsets */
526-
off_t x = 0x7fffffffUL, y = 0xffffffffUL;
527-
if (x > (x + 1) || y > (y + 1)) {
528-
munmap(idx_map, idx_size);
529-
return error("pack too large for current definition of off_t in %s", path);
530-
}
524+
if (idx_size != min_size &&
525+
/*
526+
* make sure we can deal with large pack offsets.
527+
* 31-bit signed offset won't be enough, neither
528+
* 32-bit unsigned one will be.
529+
*/
530+
(sizeof(off_t) <= 4)) {
531+
munmap(idx_map, idx_size);
532+
return error("pack too large for current definition of off_t in %s", path);
531533
}
532534
}
533535

0 commit comments

Comments
 (0)