Skip to content

Commit 9c2f8de

Browse files
tboegidscho
authored andcommitted
Use size_t instead of 'unsigned long' for data in memory
Currently the length of data which is stored in memory is stored in "unsigned long" at many places in the code base. This is OK when both "unsigned long" and size_t are 32 bits, (and is OK when both are 64 bits). On a 64 bit Windows system am "unsigned long" is 32 bit, and that may be too short to measure the size of objects in memory, a size_t is the natural choice. Improve the code base in "small steps", as small as possible. The smallest step seems to be much bigger than expected. See this code-snippet from convert.c: const char *ret; unsigned long sz; void *data = read_blob_data_from_index(istate, path, &sz); ret = gather_convert_stats_ascii(data, sz); The corrected version looks like this: const char *ret; size_t sz; void *data = read_blob_data_from_index(istate, path, &sz); ret = gather_convert_stats_ascii(data, sz); However, when the Git code base is compiled with a compiler that complains that "unsigned long" is different from size_t, we end up in this huge patch, before the code base cleanly compiles. And: there is more to be done in the zlib interface. Reviewed-by: Johannes Schindelin <[email protected]> Signed-off-by: Torsten Bögershausen <[email protected]>
1 parent 5b60f72 commit 9c2f8de

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+432
-429
lines changed

apply.c

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,9 @@ static void set_default_whitespace_mode(struct apply_state *state)
177177
* of context lines.
178178
*/
179179
struct fragment {
180-
unsigned long leading, trailing;
181-
unsigned long oldpos, oldlines;
182-
unsigned long newpos, newlines;
180+
size_t leading, trailing;
181+
size_t oldpos, oldlines;
182+
size_t newpos, newlines;
183183
/*
184184
* 'patch' is usually borrowed from buf in apply_patch(),
185185
* but some codepaths store an allocated buffer.
@@ -423,9 +423,9 @@ static int read_patch_file(struct strbuf *sb, int fd)
423423
return 0;
424424
}
425425

426-
static unsigned long linelen(const char *buffer, unsigned long size)
426+
static size_t linelen(const char *buffer, size_t size)
427427
{
428-
unsigned long len = 0;
428+
size_t len = 0;
429429
while (size--) {
430430
len++;
431431
if (*buffer++ == '\n')
@@ -1321,7 +1321,7 @@ static int parse_git_header(struct apply_state *state,
13211321
unsigned int size,
13221322
struct patch *patch)
13231323
{
1324-
unsigned long offset;
1324+
size_t offset;
13251325

13261326
/* A git diff has explicit new/delete information, so we don't guess */
13271327
patch->is_new = 0;
@@ -1391,7 +1391,7 @@ static int parse_git_header(struct apply_state *state,
13911391
return offset;
13921392
}
13931393

1394-
static int parse_num(const char *line, unsigned long *p)
1394+
static int parse_num(const char *line, size_t *p)
13951395
{
13961396
char *ptr;
13971397

@@ -1402,7 +1402,7 @@ static int parse_num(const char *line, unsigned long *p)
14021402
}
14031403

14041404
static int parse_range(const char *line, int len, int offset, const char *expect,
1405-
unsigned long *p1, unsigned long *p2)
1405+
size_t *p1, size_t *p2)
14061406
{
14071407
int digits, ex;
14081408

@@ -1517,19 +1517,19 @@ static int parse_fragment_header(const char *line, int len, struct fragment *fra
15171517
*/
15181518
static int find_header(struct apply_state *state,
15191519
const char *line,
1520-
unsigned long size,
1520+
size_t size,
15211521
int *hdrsize,
15221522
struct patch *patch)
15231523
{
1524-
unsigned long offset, len;
1524+
size_t offset, len;
15251525

15261526
patch->is_toplevel_relative = 0;
15271527
patch->is_rename = patch->is_copy = 0;
15281528
patch->is_new = patch->is_delete = -1;
15291529
patch->old_mode = patch->new_mode = 0;
15301530
patch->old_name = patch->new_name = NULL;
15311531
for (offset = 0; size > 0; offset += len, size -= len, line += len, state->linenr++) {
1532-
unsigned long nextlen;
1532+
size_t nextlen;
15331533

15341534
len = linelen(line, size);
15351535
if (!len)
@@ -1667,14 +1667,14 @@ static void check_old_for_crlf(struct patch *patch, const char *line, int len)
16671667
*/
16681668
static int parse_fragment(struct apply_state *state,
16691669
const char *line,
1670-
unsigned long size,
1670+
size_t size,
16711671
struct patch *patch,
16721672
struct fragment *fragment)
16731673
{
16741674
int added, deleted;
16751675
int len = linelen(line, size), offset;
1676-
unsigned long oldlines, newlines;
1677-
unsigned long leading, trailing;
1676+
size_t oldlines, newlines;
1677+
size_t leading, trailing;
16781678

16791679
offset = parse_fragment_header(line, len, fragment);
16801680
if (offset < 0)
@@ -1789,11 +1789,11 @@ static int parse_fragment(struct apply_state *state,
17891789
*/
17901790
static int parse_single_patch(struct apply_state *state,
17911791
const char *line,
1792-
unsigned long size,
1792+
size_t size,
17931793
struct patch *patch)
17941794
{
1795-
unsigned long offset = 0;
1796-
unsigned long oldlines = 0, newlines = 0, context = 0;
1795+
size_t offset = 0;
1796+
size_t oldlines = 0, newlines = 0, context = 0;
17971797
struct fragment **fragp = &patch->fragments;
17981798

17991799
while (size > 4 && !memcmp(line, "@@ -", 4)) {
@@ -1864,8 +1864,8 @@ static inline int metadata_changes(struct patch *patch)
18641864
patch->old_mode != patch->new_mode);
18651865
}
18661866

1867-
static char *inflate_it(const void *data, unsigned long size,
1868-
unsigned long inflated_size)
1867+
static char *inflate_it(const void *data, size_t size,
1868+
size_t inflated_size)
18691869
{
18701870
git_zstream stream;
18711871
void *out;
@@ -1894,7 +1894,7 @@ static char *inflate_it(const void *data, unsigned long size,
18941894
*/
18951895
static struct fragment *parse_binary_hunk(struct apply_state *state,
18961896
char **buf_p,
1897-
unsigned long *sz_p,
1897+
size_t *sz_p,
18981898
int *status_p,
18991899
int *used_p)
19001900
{
@@ -1911,10 +1911,10 @@ static struct fragment *parse_binary_hunk(struct apply_state *state,
19111911
* to 1-26 bytes, and 'a'-'z' corresponds to 27-52 bytes.
19121912
*/
19131913
int llen, used;
1914-
unsigned long size = *sz_p;
1914+
size_t size = *sz_p;
19151915
char *buffer = *buf_p;
19161916
int patch_method;
1917-
unsigned long origlen;
1917+
size_t origlen;
19181918
char *data = NULL;
19191919
int hunk_size = 0;
19201920
struct fragment *frag;
@@ -2006,7 +2006,7 @@ static struct fragment *parse_binary_hunk(struct apply_state *state,
20062006
*/
20072007
static int parse_binary(struct apply_state *state,
20082008
char *buffer,
2009-
unsigned long size,
2009+
size_t size,
20102010
struct patch *patch)
20112011
{
20122012
/*
@@ -2123,7 +2123,7 @@ static int use_patch(struct apply_state *state, struct patch *p)
21232123
* the number of bytes consumed otherwise,
21242124
* so that the caller can call us again for the next patch.
21252125
*/
2126-
static int parse_chunk(struct apply_state *state, char *buffer, unsigned long size, struct patch *patch)
2126+
static int parse_chunk(struct apply_state *state, char *buffer, size_t size, struct patch *patch)
21272127
{
21282128
int hdrsize, patchsize;
21292129
int offset = find_header(state, buffer, size, &hdrsize, patch);
@@ -2153,7 +2153,7 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
21532153
if (!patchsize) {
21542154
static const char git_binary[] = "GIT binary patch\n";
21552155
int hd = hdrsize + offset;
2156-
unsigned long llen = linelen(buffer + hd, size - hd);
2156+
size_t llen = linelen(buffer + hd, size - hd);
21572157

21582158
if (llen == sizeof(git_binary) - 1 &&
21592159
!memcmp(git_binary, buffer + hd, llen)) {
@@ -2397,7 +2397,7 @@ static void update_pre_post_images(struct image *preimage,
23972397
static int line_by_line_fuzzy_match(struct image *img,
23982398
struct image *preimage,
23992399
struct image *postimage,
2400-
unsigned long current,
2400+
size_t current,
24012401
int current_lno,
24022402
int preimage_limit)
24032403
{
@@ -2466,7 +2466,7 @@ static int match_fragment(struct apply_state *state,
24662466
struct image *img,
24672467
struct image *preimage,
24682468
struct image *postimage,
2469-
unsigned long current,
2469+
size_t current,
24702470
int current_lno,
24712471
unsigned ws_rule,
24722472
int match_beginning, int match_end)
@@ -2677,7 +2677,7 @@ static int find_pos(struct apply_state *state,
26772677
int match_beginning, int match_end)
26782678
{
26792679
int i;
2680-
unsigned long backwards, forwards, current;
2680+
size_t backwards, forwards, current;
26812681
int backwards_lno, forwards_lno, current_lno;
26822682

26832683
/*
@@ -2861,7 +2861,7 @@ static int apply_one_fragment(struct apply_state *state,
28612861
int new_blank_lines_at_end = 0;
28622862
int found_new_blank_lines_at_end = 0;
28632863
int hunk_linenr = frag->linenr;
2864-
unsigned long leading, trailing;
2864+
size_t leading, trailing;
28652865
int pos, applied_pos;
28662866
struct image preimage;
28672867
struct image postimage;
@@ -3085,9 +3085,9 @@ static int apply_one_fragment(struct apply_state *state,
30853085
*/
30863086
if ((leading != frag->leading ||
30873087
trailing != frag->trailing) && state->apply_verbosity > verbosity_silent)
3088-
fprintf_ln(stderr, _("Context reduced to (%ld/%ld)"
3088+
fprintf_ln(stderr, _("Context reduced to (%"PRIuMAX"/%"PRIuMAX")"
30893089
" to apply fragment at %d"),
3090-
leading, trailing, applied_pos+1);
3090+
(uintmax_t)leading, (uintmax_t)trailing, applied_pos+1);
30913091
update_image(state, img, applied_pos, &preimage, &postimage);
30923092
} else {
30933093
if (state->apply_verbosity > verbosity_normal)
@@ -3109,7 +3109,7 @@ static int apply_binary_fragment(struct apply_state *state,
31093109
struct patch *patch)
31103110
{
31113111
struct fragment *fragment = patch->fragments;
3112-
unsigned long len;
3112+
size_t len;
31133113
void *dst;
31143114

31153115
if (!fragment)
@@ -3199,7 +3199,7 @@ static int apply_binary(struct apply_state *state,
31993199
if (has_object_file(&oid)) {
32003200
/* We already have the postimage */
32013201
enum object_type type;
3202-
unsigned long size;
3202+
size_t size;
32033203
char *result;
32043204

32053205
result = read_object_file(&oid, &type, &size);
@@ -3244,7 +3244,7 @@ static int apply_fragments(struct apply_state *state, struct image *img, struct
32443244
while (frag) {
32453245
nth++;
32463246
if (apply_one_fragment(state, img, frag, inaccurate_eof, ws_rule, nth)) {
3247-
error(_("patch failed: %s:%ld"), name, frag->oldpos);
3247+
error(_("patch failed: %s:%"PRIuMAX), name, (uintmax_t)frag->oldpos);
32483248
if (!state->apply_with_reject)
32493249
return -1;
32503250
frag->rejected = 1;
@@ -3261,7 +3261,7 @@ static int read_blob_object(struct strbuf *buf, const struct object_id *oid, uns
32613261
strbuf_addf(buf, "Subproject commit %s\n", oid_to_hex(oid));
32623262
} else {
32633263
enum object_type type;
3264-
unsigned long sz;
3264+
size_t sz;
32653265
char *result;
32663266

32673267
result = read_object_file(oid, &type, &sz);
@@ -4290,7 +4290,7 @@ static int add_index_file(struct apply_state *state,
42904290
const char *path,
42914291
unsigned mode,
42924292
void *buf,
4293-
unsigned long size)
4293+
size_t size)
42944294
{
42954295
struct stat st;
42964296
struct cache_entry *ce;
@@ -4344,7 +4344,7 @@ static int add_index_file(struct apply_state *state,
43444344
*/
43454345
static int try_create_file(struct apply_state *state, const char *path,
43464346
unsigned int mode, const char *buf,
4347-
unsigned long size)
4347+
size_t size)
43484348
{
43494349
int fd, res;
43504350
struct strbuf nbuf = STRBUF_INIT;
@@ -4395,7 +4395,7 @@ static int create_one_file(struct apply_state *state,
43954395
char *path,
43964396
unsigned mode,
43974397
const char *buf,
4398-
unsigned long size)
4398+
size_t size)
43994399
{
44004400
int res;
44014401

@@ -4487,7 +4487,7 @@ static int create_file(struct apply_state *state, struct patch *patch)
44874487
{
44884488
char *path = patch->new_name;
44894489
unsigned mode = patch->new_mode;
4490-
unsigned long size = patch->resultsize;
4490+
size_t size = patch->resultsize;
44914491
char *buf = patch->result;
44924492

44934493
if (!mode)

archive-tar.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#define BLOCKSIZE (RECORDSIZE * 20)
1414

1515
static char block[BLOCKSIZE];
16-
static unsigned long offset;
16+
static size_t offset;
1717

1818
static int tar_umask = 002;
1919

@@ -63,12 +63,12 @@ static void write_if_needed(void)
6363
* queues up writes, so that all our write(2) calls write exactly one
6464
* full block; pads writes to RECORDSIZE
6565
*/
66-
static void do_write_blocked(const void *data, unsigned long size)
66+
static void do_write_blocked(const void *data, size_t size)
6767
{
6868
const char *buf = data;
6969

7070
if (offset) {
71-
unsigned long chunk = BLOCKSIZE - offset;
71+
size_t chunk = BLOCKSIZE - offset;
7272
if (size < chunk)
7373
chunk = size;
7474
memcpy(block + offset, buf, chunk);
@@ -90,7 +90,7 @@ static void do_write_blocked(const void *data, unsigned long size)
9090

9191
static void finish_record(void)
9292
{
93-
unsigned long tail;
93+
size_t tail;
9494
tail = offset % RECORDSIZE;
9595
if (tail) {
9696
memset(block + offset, 0, RECORDSIZE - tail);
@@ -99,7 +99,7 @@ static void finish_record(void)
9999
write_if_needed();
100100
}
101101

102-
static void write_blocked(const void *data, unsigned long size)
102+
static void write_blocked(const void *data, size_t size)
103103
{
104104
do_write_blocked(data, size);
105105
finish_record();
@@ -128,7 +128,7 @@ static int stream_blocked(const struct object_id *oid)
128128
{
129129
struct git_istream *st;
130130
enum object_type type;
131-
unsigned long sz;
131+
size_t sz;
132132
char buf[BLOCKSIZE];
133133
ssize_t readlen;
134134

@@ -211,7 +211,7 @@ static size_t get_path_prefix(const char *path, size_t pathlen, size_t maxlen)
211211

212212
static void prepare_header(struct archiver_args *args,
213213
struct ustar_header *header,
214-
unsigned int mode, unsigned long size)
214+
unsigned int mode, size_t size)
215215
{
216216
xsnprintf(header->mode, sizeof(header->mode), "%07o", mode & 07777);
217217
xsnprintf(header->size, sizeof(header->size), "%011"PRIoMAX , S_ISREG(mode) ? (uintmax_t)size : (uintmax_t)0);
@@ -232,7 +232,7 @@ static void prepare_header(struct archiver_args *args,
232232

233233
static void write_extended_header(struct archiver_args *args,
234234
const struct object_id *oid,
235-
const void *buffer, unsigned long size)
235+
const void *buffer, size_t size)
236236
{
237237
struct ustar_header header;
238238
unsigned int mode;
@@ -253,7 +253,7 @@ static int write_tar_entry(struct archiver_args *args,
253253
struct ustar_header header;
254254
struct strbuf ext_header = STRBUF_INIT;
255255
unsigned int old_mode = mode;
256-
unsigned long size, size_in_header;
256+
size_t size, size_in_header;
257257
void *buffer;
258258
int err = 0;
259259

archive-zip.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ static int write_zip_entry(struct archiver_args *args,
297297
void *buffer;
298298
struct git_istream *stream = NULL;
299299
unsigned long flags = 0;
300-
unsigned long size;
300+
size_t size;
301301
int is_binary = -1;
302302
const char *path_without_prefix = path + args->baselen;
303303
unsigned int creator_version = 0;

archive.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ static void format_subst(const struct commit *commit,
7373
void *object_file_to_archive(const struct archiver_args *args,
7474
const char *path, const struct object_id *oid,
7575
unsigned int mode, enum object_type *type,
76-
unsigned long *sizep)
76+
size_t *sizep)
7777
{
7878
void *buffer;
7979
const struct commit *commit = args->convert ? args->commit : NULL;

archive.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ typedef int (*write_archive_entry_fn_t)(struct archiver_args *args,
5252

5353
int write_archive_entries(struct archiver_args *args, write_archive_entry_fn_t write_entry);
5454
void *object_file_to_archive(const struct archiver_args *args,
55-
const char *path, const struct object_id *oid,
56-
unsigned int mode, enum object_type *type,
57-
unsigned long *sizep);
55+
const char *path, const struct object_id *oid,
56+
unsigned int mode, enum object_type *type,
57+
size_t *sizep);
5858

5959
#endif /* ARCHIVE_H */

0 commit comments

Comments
 (0)