@matheusbsbs / matheus-bagliano / commits / 8723f21626
Fork 0

make overflow test on delta base offset work regardless of variable size

This patch introduces the MSB() macro to obtain the desired number of most significant bits from a given variable independently of the variable type. It is then used to better implement the overflow test on the OBJ_OFS_DELTA base offset variable with the property of always working correctly regardless of the type/size of that variable. Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Nicolas Pitre committed Apr 9, 2007 at 01:06 UTC 8723f216263ba4a0f06be7b93fada863c0931e09
5 files changed +12 -4
builtin-pack-objects.c
+1 -1
@@ -1014,7 +1014,7 @@ static void check_object(struct object_entry *entry)
1014 ofs = c & 127;
1015 while (c & 128) {
1016 ofs += 1;
1017 - if (!ofs || ofs & ~(~0UL >> 7))
1017 + if (!ofs || MSB(ofs, 7))
1018 die("delta base offset overflow in pack for %s",
1019 sha1_to_hex(entry->sha1));
1020 c = buf[used_0++];
builtin-unpack-objects.c
+1 -1
@@ -209,7 +209,7 @@ static void unpack_delta_entry(enum object_type type, unsigned long delta_size,
209 base_offset = c & 127;
210 while (c & 128) {
211 base_offset += 1;
212 - if (!base_offset || base_offset & ~(~0UL >> 7))
212 + if (!base_offset || MSB(base_offset, 7))
213 die("offset value overflow for delta base object");
214 pack = fill(1);
215 c = *pack;
git-compat-util.h
+8
@@ -13,6 +13,14 @@
13
14 #define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
15
16 +#ifdef __GNUC__
17 +#define TYPEOF(x) (__typeof__(x))
18 +#else
19 +#define TYPEOF(x)
20 +#endif
21 +
22 +#define MSB(x, bits) ((x) & TYPEOF(x)(~0ULL << (sizeof(x) * 8 - (bits))))
23 +
24 #if !defined(__APPLE__) && !defined(__FreeBSD__)
25 #define _XOPEN_SOURCE 600 /* glibc2 and AIX 5.3L need 500, OpenBSD needs 600 for S_ISLNK() */
26 #define _XOPEN_SOURCE_EXTENDED 1 /* AIX 5.3L needs this */
index-pack.c
+1 -1
@@ -249,7 +249,7 @@ static void *unpack_raw_entry(struct object_entry *obj, union delta_base *delta_
249 base_offset = c & 127;
250 while (c & 128) {
251 base_offset += 1;
252 - if (!base_offset || base_offset & ~(~0UL >> 7))
252 + if (!base_offset || MSB(base_offset, 7))
253 bad_object(obj->offset, "offset value overflow for delta base object");
254 p = fill(1);
255 c = *p;
sha1_file.c
+1 -1
@@ -1150,7 +1150,7 @@ static off_t get_delta_base(struct packed_git *p,
1150 base_offset = c & 127;
1151 while (c & 128) {
1152 base_offset += 1;
1153 - if (!base_offset || base_offset & ~(~0UL >> 7))
1153 + if (!base_offset || MSB(base_offset, 7))
1154 die("offset value overflow for delta base object");
1155 c = base_info[used++];
1156 base_offset = (base_offset << 7) + (c & 127);