Skip to content

Commit f9b622e

Browse files
vtbassmattdscho
authored andcommitted
Guard against data loss checking out a huge file
This introduces an additional guard for platforms where `unsigned long` and `size_t` are not of the same size. If the size of an object in the database would overflow `unsigned long`, instead we now exit with an error. A complete fix will have to update _many_ other functions throughout the codebase to use `size_t` instead of `unsigned long`. It will have to be implemented at some stage. This commit puts in a stop-gap for the time being. Signed-off-by: Matt Cooper <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 8bd3286 commit f9b622e

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

delta.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,15 @@ 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 long cmd, size = 0;
93+
size_t cmd, size = 0;
9494
int i = 0;
9595
do {
9696
cmd = *data++;
97-
size |= (cmd & 0x7f) << i;
97+
size |= st_left_shift(cmd & 0x7f, i);
9898
i += 7;
9999
} while (cmd & 0x80 && data < top);
100100
*datap = data;
101-
return size;
101+
return cast_size_t_to_ulong(size);
102102
}
103103

104104
#endif

object-file.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,7 +1367,7 @@ static int parse_loose_header_extended(const char *hdr, struct object_info *oi,
13671367
unsigned int flags)
13681368
{
13691369
const char *type_buf = hdr;
1370-
unsigned long size;
1370+
size_t size;
13711371
int type, type_len = 0;
13721372

13731373
/*
@@ -1411,12 +1411,12 @@ static int parse_loose_header_extended(const char *hdr, struct object_info *oi,
14111411
if (c > 9)
14121412
break;
14131413
hdr++;
1414-
size = size * 10 + c;
1414+
size = st_add(st_mult(size, 10), c);
14151415
}
14161416
}
14171417

14181418
if (oi->sizep)
1419-
*oi->sizep = size;
1419+
*oi->sizep = cast_size_t_to_ulong(size);
14201420

14211421
/*
14221422
* The length must be followed by a zero byte

packfile.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,7 +1059,7 @@ unsigned long unpack_object_header_buffer(const unsigned char *buf,
10591059
unsigned long len, enum object_type *type, unsigned long *sizep)
10601060
{
10611061
unsigned shift;
1062-
unsigned long size, c;
1062+
size_t size, c;
10631063
unsigned long used = 0;
10641064

10651065
c = buf[used++];
@@ -1073,10 +1073,10 @@ unsigned long unpack_object_header_buffer(const unsigned char *buf,
10731073
break;
10741074
}
10751075
c = buf[used++];
1076-
size += (c & 0x7f) << shift;
1076+
size = st_add(size, st_left_shift(c & 0x7f, shift));
10771077
shift += 7;
10781078
}
1079-
*sizep = size;
1079+
*sizep = cast_size_t_to_ulong(size);
10801080
return used;
10811081
}
10821082

0 commit comments

Comments
 (0)