Skip to content

Commit bd465d7

Browse files
authored
Merge pull request #3487 from vtbassmatt/huge-file-smudge-clean
Teach Git to handle huge files in smudge/clean
2 parents f8f57df + acc5591 commit bd465d7

11 files changed

Lines changed: 88 additions & 19 deletions

‎convert.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ static int crlf_to_worktree(const char *src, size_t len, struct strbuf *buf,
613613

614614
struct filter_params {
615615
const char *src;
616-
unsigned long size;
616+
size_t size;
617617
int fd;
618618
const char *cmd;
619619
const char *path;

‎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

‎entry.c‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,13 @@ static int create_file(const char *path, unsigned int mode)
8282
return open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
8383
}
8484

85-
void *read_blob_entry(const struct cache_entry *ce, unsigned long *size)
85+
void *read_blob_entry(const struct cache_entry *ce, size_t *size)
8686
{
8787
enum object_type type;
88-
void *blob_data = read_object_file(&ce->oid, &type, size);
88+
unsigned long ul;
89+
void *blob_data = read_object_file(&ce->oid, &type, &ul);
8990

91+
*size = ul;
9092
if (blob_data) {
9193
if (type == OBJ_BLOB)
9294
return blob_data;
@@ -273,7 +275,7 @@ static int write_entry(struct cache_entry *ce, char *path, struct conv_attrs *ca
273275
int fd, ret, fstat_done = 0;
274276
char *new_blob;
275277
struct strbuf buf = STRBUF_INIT;
276-
unsigned long size;
278+
size_t size;
277279
ssize_t wrote;
278280
size_t newsize = 0;
279281
struct stat st;

‎entry.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ int finish_delayed_checkout(struct checkout *state, int *nr_checkouts,
5252
*/
5353
void unlink_entry(const struct cache_entry *ce);
5454

55-
void *read_blob_entry(const struct cache_entry *ce, unsigned long *size);
55+
void *read_blob_entry(const struct cache_entry *ce, size_t *size);
5656
int fstat_checkout_output(int fd, const struct checkout *state, struct stat *st);
5757
void update_ce_after_write(const struct checkout *state, struct cache_entry *ce,
5858
struct stat *st);

‎git-compat-util.h‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,14 @@
113113
#define unsigned_mult_overflows(a, b) \
114114
((a) && (b) > maximum_unsigned_value_of_type(a) / (a))
115115

116+
/*
117+
* Returns true if the left shift of "a" by "shift" bits will
118+
* overflow. The type of "a" must be unsigned.
119+
*/
120+
#define unsigned_left_shift_overflows(a, shift) \
121+
((shift) < bitsizeof(a) && \
122+
(a) > maximum_unsigned_value_of_type(a) >> (shift))
123+
116124
#ifdef __GNUC__
117125
#define TYPEOF(x) (__typeof__(x))
118126
#else
@@ -880,6 +888,23 @@ static inline size_t st_sub(size_t a, size_t b)
880888
return a - b;
881889
}
882890

891+
static inline size_t st_left_shift(size_t a, unsigned shift)
892+
{
893+
if (unsigned_left_shift_overflows(a, shift))
894+
die("size_t overflow: %"PRIuMAX" << %u",
895+
(uintmax_t)a, shift);
896+
return a << shift;
897+
}
898+
899+
static inline unsigned long cast_size_t_to_ulong(size_t a)
900+
{
901+
if (a != (unsigned long)a)
902+
die("object too large to read on this platform: %"
903+
PRIuMAX" is cut off to %lu",
904+
(uintmax_t)a, (unsigned long)a);
905+
return (unsigned long)a;
906+
}
907+
883908
#ifdef HAVE_ALLOCA_H
884909
# include <alloca.h>
885910
# define xalloca(size) (alloca(size))

‎object-file.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,7 +1410,7 @@ static int parse_loose_header_extended(const char *hdr, struct object_info *oi,
14101410
unsigned int flags)
14111411
{
14121412
const char *type_buf = hdr;
1413-
unsigned long size;
1413+
size_t size;
14141414
int type, type_len = 0;
14151415

14161416
/*
@@ -1454,12 +1454,12 @@ static int parse_loose_header_extended(const char *hdr, struct object_info *oi,
14541454
if (c > 9)
14551455
break;
14561456
hdr++;
1457-
size = size * 10 + c;
1457+
size = st_add(st_mult(size, 10), c);
14581458
}
14591459
}
14601460

14611461
if (oi->sizep)
1462-
*oi->sizep = size;
1462+
*oi->sizep = cast_size_t_to_ulong(size);
14631463

14641464
/*
14651465
* 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

‎parallel-checkout.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ static int write_pc_item_to_fd(struct parallel_checkout_item *pc_item, int fd,
261261
struct stream_filter *filter;
262262
struct strbuf buf = STRBUF_INIT;
263263
char *blob;
264-
unsigned long size;
264+
size_t size;
265265
ssize_t wrote;
266266

267267
/* Sanity check */

‎t/helper/test-genzeros.c‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,31 @@
33

44
int cmd__genzeros(int argc, const char **argv)
55
{
6-
long count;
6+
/* static, so that it is NUL-initialized */
7+
static const char zeros[256 * 1024];
8+
intmax_t count;
9+
ssize_t n;
710

811
if (argc > 2) {
912
fprintf(stderr, "usage: %s [<count>]\n", argv[0]);
1013
return 1;
1114
}
1215

13-
count = argc > 1 ? strtol(argv[1], NULL, 0) : -1L;
16+
count = argc > 1 ? strtoimax(argv[1], NULL, 0) : -1;
1417

15-
while (count < 0 || count--) {
16-
if (putchar(0) == EOF)
18+
/* Writing out individual NUL bytes is slow... */
19+
while (count < 0)
20+
if (write(1, zeros, ARRAY_SIZE(zeros)) < 0)
1721
return -1;
22+
23+
while (count > 0) {
24+
n = write(1, zeros, count < ARRAY_SIZE(zeros) ?
25+
count : ARRAY_SIZE(zeros));
26+
27+
if (n < 0)
28+
return -1;
29+
30+
count -= n;
1831
}
1932

2033
return 0;

‎t/t1051-large-conversion.sh‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,29 @@ test_expect_success 'ident converts on output' '
8383
test_cmp small.clean large.clean
8484
'
8585

86+
# This smudge filter prepends 5GB of zeros to the file it checks out. This
87+
# ensures that smudging doesn't mangle large files on 64-bit Windows.
88+
test_expect_success EXPENSIVE,SIZE_T_IS_64BIT,!LONG_IS_64BIT \
89+
'files over 4GB convert on output' '
90+
test_commit test small "a small file" &&
91+
test_config filter.makelarge.smudge \
92+
"test-tool genzeros $((5*1024*1024*1024)) && cat" &&
93+
echo "small filter=makelarge" >.gitattributes &&
94+
rm small &&
95+
git checkout -- small &&
96+
size=$(test_file_size small) &&
97+
test "$size" -ge $((5 * 1024 * 1024 * 1024))
98+
'
99+
100+
# This clean filter writes down the size of input it receives. By checking against
101+
# the actual size, we ensure that cleaning doesn't mangle large files on 64-bit Windows.
102+
test_expect_success EXPENSIVE,SIZE_T_IS_64BIT,!LONG_IS_64BIT \
103+
'files over 4GB convert on input' '
104+
test-tool genzeros $((5*1024*1024*1024)) >big &&
105+
test_config filter.checklarge.clean "wc -c >big.size" &&
106+
echo "big filter=checklarge" >.gitattributes &&
107+
git add big &&
108+
test $(test_file_size big) -eq $(cat big.size)
109+
'
110+
86111
test_done

0 commit comments

Comments
 (0)