Skip to content

Commit 48fb7de

Browse files
torvaldsgitster
authored andcommitted
Fix big left-shifts of unsigned char
Shifting 'unsigned char' or 'unsigned short' left can result in sign extension errors, since the C integer promotion rules means that the unsigned char/short will get implicitly promoted to a signed 'int' due to the shift (or due to other operations). This normally doesn't matter, but if you shift things up sufficiently, it will now set the sign bit in 'int', and a subsequent cast to a bigger type (eg 'long' or 'unsigned long') will now sign-extend the value despite the original expression being unsigned. One example of this would be something like unsigned long size; unsigned char c; size += c << 24; where despite all the variables being unsigned, 'c << 24' ends up being a signed entity, and will get sign-extended when then doing the addition in an 'unsigned long' type. Since git uses 'unsigned char' pointers extensively, we actually have this bug in a couple of places. I may have missed some, but this is the result of looking at git grep '[^0-9 ][ ]*<<[ ][a-z]' -- '*.c' '*.h' git grep '<<[ ]*24' which catches at least the common byte cases (shifting variables by a variable amount, and shifting by 24 bits). I also grepped for just 'unsigned char' variables in general, and converted the ones that most obviously ended up getting implicitly cast immediately anyway (eg hash_name(), encode_85()). In addition to just avoiding 'unsigned char', this patch also tries to use a common idiom for the delta header size thing. We had three different variations on it: "& 0x7fUL" in one place (getting the sign extension right), and "& ~0x80" and "& 0x7f" in two other places (not getting it right). Apart from making them all just avoid using "unsigned char" at all, I also unified them to then use a simple "& 0x7f". I considered making a sparse extension which warns about doing implicit casts from unsigned types to signed types, but it gets rather complex very quickly, so this is just a hack. Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 50a991e commit 48fb7de

File tree

8 files changed

+12
-16
lines changed

8 files changed

+12
-16
lines changed

attr.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ static struct git_attr *(git_attr_hash[HASHSIZE]);
3535

3636
static unsigned hash_name(const char *name, int namelen)
3737
{
38-
unsigned val = 0;
39-
unsigned char c;
38+
unsigned val = 0, c;
4039

4140
while (namelen--) {
4241
c = *name++;

base85.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ void encode_85(char *buf, const unsigned char *data, int bytes)
9191
unsigned acc = 0;
9292
int cnt;
9393
for (cnt = 24; cnt >= 0; cnt -= 8) {
94-
int ch = *data++;
94+
unsigned ch = *data++;
9595
acc |= ch << cnt;
9696
if (--bytes == 0)
9797
break;

builtin-pack-objects.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -653,8 +653,7 @@ static void rehash_objects(void)
653653

654654
static unsigned name_hash(const char *name)
655655
{
656-
unsigned char c;
657-
unsigned hash = 0;
656+
unsigned c, hash = 0;
658657

659658
if (!name)
660659
return 0;

builtin-unpack-objects.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,8 +422,8 @@ static void unpack_delta_entry(enum object_type type, unsigned long delta_size,
422422
static void unpack_one(unsigned nr)
423423
{
424424
unsigned shift;
425-
unsigned char *pack, c;
426-
unsigned long size;
425+
unsigned char *pack;
426+
unsigned long size, c;
427427
enum object_type type;
428428

429429
obj_list[nr].offset = consumed_bytes;

delta.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,11 @@ static inline unsigned long get_delta_hdr_size(const unsigned char **datap,
9090
const unsigned char *top)
9191
{
9292
const unsigned char *data = *datap;
93-
unsigned char cmd;
94-
unsigned long size = 0;
93+
unsigned long cmd, size = 0;
9594
int i = 0;
9695
do {
9796
cmd = *data++;
98-
size |= (cmd & ~0x80) << i;
97+
size |= (cmd & 0x7f) << i;
9998
i += 7;
10099
} while (cmd & 0x80 && data < top);
101100
*datap = data;

index-pack.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,8 @@ static void *unpack_entry_data(unsigned long offset, unsigned long size)
293293

294294
static void *unpack_raw_entry(struct object_entry *obj, union delta_base *delta_base)
295295
{
296-
unsigned char *p, c;
297-
unsigned long size;
296+
unsigned char *p;
297+
unsigned long size, c;
298298
off_t base_offset;
299299
unsigned shift;
300300
void *data;
@@ -312,7 +312,7 @@ static void *unpack_raw_entry(struct object_entry *obj, union delta_base *delta_
312312
p = fill(1);
313313
c = *p;
314314
use(1);
315-
size += (c & 0x7fUL) << shift;
315+
size += (c & 0x7f) << shift;
316316
shift += 7;
317317
}
318318
obj->size = size;

patch-delta.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void *patch_delta(const void *src_buf, unsigned long src_size,
4444
if (cmd & 0x01) cp_off = *data++;
4545
if (cmd & 0x02) cp_off |= (*data++ << 8);
4646
if (cmd & 0x04) cp_off |= (*data++ << 16);
47-
if (cmd & 0x08) cp_off |= (*data++ << 24);
47+
if (cmd & 0x08) cp_off |= ((unsigned) *data++ << 24);
4848
if (cmd & 0x10) cp_size = *data++;
4949
if (cmd & 0x20) cp_size |= (*data++ << 8);
5050
if (cmd & 0x40) cp_size |= (*data++ << 16);

sha1_file.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,8 +1162,7 @@ unsigned long unpack_object_header_buffer(const unsigned char *buf,
11621162
unsigned long len, enum object_type *type, unsigned long *sizep)
11631163
{
11641164
unsigned shift;
1165-
unsigned char c;
1166-
unsigned long size;
1165+
unsigned long size, c;
11671166
unsigned long used = 0;
11681167

11691168
c = buf[used++];

0 commit comments

Comments
 (0)