Skip to content

Commit 1e03c75

Browse files
vtbassmattdscho
authored andcommitted
odb: 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. Helped-by: Johannes Schindelin <[email protected]> Signed-off-by: Matt Cooper <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent f5585dd commit 1e03c75

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
@@ -1306,7 +1306,7 @@ static void *unpack_loose_rest(git_zstream *stream,
13061306
int parse_loose_header(const char *hdr, struct object_info *oi)
13071307
{
13081308
const char *type_buf = hdr;
1309-
unsigned long size;
1309+
size_t size;
13101310
int type, type_len = 0;
13111311

13121312
/*
@@ -1341,12 +1341,12 @@ int parse_loose_header(const char *hdr, struct object_info *oi)
13411341
if (c > 9)
13421342
break;
13431343
hdr++;
1344-
size = size * 10 + c;
1344+
size = st_add(st_mult(size, 10), c);
13451345
}
13461346
}
13471347

13481348
if (oi->sizep)
1349-
*oi->sizep = size;
1349+
*oi->sizep = cast_size_t_to_ulong(size);
13501350

13511351
/*
13521352
* 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
@@ -1060,7 +1060,7 @@ unsigned long unpack_object_header_buffer(const unsigned char *buf,
10601060
unsigned long len, enum object_type *type, unsigned long *sizep)
10611061
{
10621062
unsigned shift;
1063-
unsigned long size, c;
1063+
size_t size, c;
10641064
unsigned long used = 0;
10651065

10661066
c = buf[used++];
@@ -1074,10 +1074,10 @@ unsigned long unpack_object_header_buffer(const unsigned char *buf,
10741074
break;
10751075
}
10761076
c = buf[used++];
1077-
size += (c & 0x7f) << shift;
1077+
size = st_add(size, st_left_shift(c & 0x7f, shift));
10781078
shift += 7;
10791079
}
1080-
*sizep = size;
1080+
*sizep = cast_size_t_to_ulong(size);
10811081
return used;
10821082
}
10831083

0 commit comments

Comments
 (0)