Upcast size_t variables to uintmax_t when printing

When printing variables which contain a size, today "unsigned long" is used at many places. In order to be able to change the type from "unsigned long" into size_t some day in the future, we need to have a way to print 64 bit variables on a system that has "unsigned long" defined to be 32 bit, like Win64. Upcast all those variables into uintmax_t before they are printed. This is to prepare for a bigger change, when "unsigned long" will be converted into size_t for variables which may be > 4Gib. Signed-off-by: Torsten Bögershausen <tboegi@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Torsten Bögershausen committed Nov 11, 2018 at 08:05 UTC ca473cef9140f55d68d6758fe226ef47c216669c
11 files changed +24 -23
archive-tar.c
+1 -1
@@ -202,7 +202,7 @@ static void prepare_header(struct archiver_args *args,
202 unsigned int mode, unsigned long size)
203 {
204 xsnprintf(header->mode, sizeof(header->mode), "%07o", mode & 07777);
205 - xsnprintf(header->size, sizeof(header->size), "%011lo", S_ISREG(mode) ? size : 0);
205 + xsnprintf(header->size, sizeof(header->size), "%011"PRIoMAX , S_ISREG(mode) ? (uintmax_t)size : (uintmax_t)0);
206 xsnprintf(header->mtime, sizeof(header->mtime), "%011lo", (unsigned long) args->time);
207
208 xsnprintf(header->uid, sizeof(header->uid), "%07o", 0);
builtin/cat-file.c
+2 -2
@@ -92,7 +92,7 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,
92 oi.sizep = &size;
93 if (oid_object_info_extended(the_repository, &oid, &oi, flags) < 0)
94 die("git cat-file: could not get object info");
95 - printf("%lu\n", size);
95 + printf("%"PRIuMAX"\n", (uintmax_t)size);
96 return 0;
97
98 case 'e':
@@ -238,7 +238,7 @@ static void expand_atom(struct strbuf *sb, const char *atom, int len,
238 if (data->mark_query)
239 data->info.sizep = &data->size;
240 else
241 - strbuf_addf(sb, "%lu", data->size);
241 + strbuf_addf(sb, "%"PRIuMAX , (uintmax_t)data->size);
242 } else if (is_atom("objectsize:disk", atom, len)) {
243 if (data->mark_query)
244 data->info.disk_sizep = &data->disk_size;
builtin/fast-export.c
+1 -1
@@ -253,7 +253,7 @@ static void export_blob(const struct object_id *oid)
253
254 mark_next_object(object);
255
256 - printf("blob\nmark :%"PRIu32"\ndata %lu\n", last_idnum, size);
256 + printf("blob\nmark :%"PRIu32"\ndata %"PRIuMAX"\n", last_idnum, (uintmax_t)size);
257 if (size && fwrite(buf, size, 1, stdout) != 1)
258 die_errno("could not write blob '%s'", oid_to_hex(oid));
259 printf("\n");
builtin/index-pack.c
+5 -4
@@ -450,7 +450,8 @@ static void *unpack_entry_data(off_t offset, unsigned long size,
450 int hdrlen;
451
452 if (!is_delta_type(type)) {
453 - hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %lu", type_name(type), size) + 1;
453 + hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %"PRIuMAX,
454 + type_name(type),(uintmax_t)size) + 1;
455 the_hash_algo->init_fn(&c);
456 the_hash_algo->update_fn(&c, hdr, hdrlen);
457 } else
@@ -1628,10 +1629,10 @@ static void show_pack_info(int stat_only)
1629 chain_histogram[obj_stat[i].delta_depth - 1]++;
1630 if (stat_only)
1631 continue;
1631 - printf("%s %-6s %lu %lu %"PRIuMAX,
1632 + printf("%s %-6s %"PRIuMAX" %"PRIuMAX" %"PRIuMAX,
1633 oid_to_hex(&obj->idx.oid),
1633 - type_name(obj->real_type), obj->size,
1634 - (unsigned long)(obj[1].idx.offset - obj->idx.offset),
1634 + type_name(obj->real_type), (uintmax_t)obj->size,
1635 + (uintmax_t)(obj[1].idx.offset - obj->idx.offset),
1636 (uintmax_t)obj->idx.offset);
1637 if (is_delta_type(obj->type)) {
1638 struct object_entry *bobj = &objects[obj_stat[i].base_object_no];
builtin/ls-tree.c
+1 -1
@@ -100,7 +100,7 @@ static int show_tree(const struct object_id *oid, struct strbuf *base,
100 "BAD");
101 else
102 xsnprintf(size_text, sizeof(size_text),
103 - "%lu", size);
103 + "%"PRIuMAX, (uintmax_t)size);
104 } else
105 xsnprintf(size_text, sizeof(size_text), "-");
106 printf("%06o %s %s %7s\t", mode, type,
builtin/pack-objects.c
+6 -6
@@ -2095,9 +2095,9 @@ static int try_delta(struct unpacked *trg, struct unpacked *src,
2095 die(_("object %s cannot be read"),
2096 oid_to_hex(&trg_entry->idx.oid));
2097 if (sz != trg_size)
2098 - die(_("object %s inconsistent object length (%lu vs %lu)"),
2099 - oid_to_hex(&trg_entry->idx.oid), sz,
2100 - trg_size);
2098 + die(_("object %s inconsistent object length (%"PRIuMAX" vs %"PRIuMAX")"),
2099 + oid_to_hex(&trg_entry->idx.oid), (uintmax_t)sz,
2100 + (uintmax_t)trg_size);
2101 *mem_usage += sz;
2102 }
2103 if (!src->data) {
@@ -2122,9 +2122,9 @@ static int try_delta(struct unpacked *trg, struct unpacked *src,
2122 oid_to_hex(&src_entry->idx.oid));
2123 }
2124 if (sz != src_size)
2125 - die(_("object %s inconsistent object length (%lu vs %lu)"),
2126 - oid_to_hex(&src_entry->idx.oid), sz,
2127 - src_size);
2125 + die(_("object %s inconsistent object length (%"PRIuMAX" vs %"PRIuMAX")"),
2126 + oid_to_hex(&src_entry->idx.oid), (uintmax_t)sz,
2127 + (uintmax_t)src_size);
2128 *mem_usage += sz;
2129 }
2130 if (!src->index) {
diff.c
+1 -1
@@ -3227,7 +3227,7 @@ static void emit_binary_diff_body(struct diff_options *o,
3227 }
3228
3229 if (delta && delta_size < deflate_size) {
3230 - char *s = xstrfmt("%lu", orig_size);
3230 + char *s = xstrfmt("%"PRIuMAX , (uintmax_t)orig_size);
3231 emit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA,
3232 s, strlen(s), 0);
3233 free(s);
fast-import.c
+2 -2
@@ -2955,8 +2955,8 @@ static void cat_blob(struct object_entry *oe, struct object_id *oid)
2955 die("Object %s is a %s but a blob was expected.",
2956 oid_to_hex(oid), type_name(type));
2957 strbuf_reset(&line);
2958 - strbuf_addf(&line, "%s %s %lu\n", oid_to_hex(oid),
2959 - type_name(type), size);
2958 + strbuf_addf(&line, "%s %s %"PRIuMAX"\n", oid_to_hex(oid),
2959 + type_name(type), (uintmax_t)size);
2960 cat_blob_write(line.buf, line.len);
2961 strbuf_release(&line);
2962 cat_blob_write(buf, size);
http-push.c
+1 -1
@@ -365,7 +365,7 @@ static void start_put(struct transfer_request *request)
365 git_zstream stream;
366
367 unpacked = read_object_file(&request->obj->oid, &type, &len);
368 - hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %lu", type_name(type), len) + 1;
368 + hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %"PRIuMAX , type_name(type), (uintmax_t)len) + 1;
369
370 /* Set it up */
371 git_deflate_init(&stream, zlib_compression_level);
ref-filter.c
+1 -1
@@ -878,7 +878,7 @@ static void grab_common_values(struct atom_value *val, int deref, struct expand_
878 v->s = xstrdup(type_name(oi->type));
879 else if (!strcmp(name, "objectsize")) {
880 v->value = oi->size;
881 - v->s = xstrfmt("%lu", oi->size);
881 + v->s = xstrfmt("%"PRIuMAX , (uintmax_t)oi->size);
882 }
883 else if (deref)
884 grab_objectname(name, &oi->oid, v, &used_atom[i]);
sha1-file.c
+3 -3
@@ -833,7 +833,7 @@ int check_object_signature(const struct object_id *oid, void *map,
833 return -1;
834
835 /* Generate the header */
836 - hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %lu", type_name(obj_type), size) + 1;
836 + hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %"PRIuMAX , type_name(obj_type), (uintmax_t)size) + 1;
837
838 /* Sha1.. */
839 the_hash_algo->init_fn(&c);
@@ -1492,7 +1492,7 @@ static void write_object_file_prepare(const void *buf, unsigned long len,
1492 git_hash_ctx c;
1493
1494 /* Generate the header */
1495 - *hdrlen = xsnprintf(hdr, *hdrlen, "%s %lu", type, len)+1;
1495 + *hdrlen = xsnprintf(hdr, *hdrlen, "%s %"PRIuMAX , type, (uintmax_t)len)+1;
1496
1497 /* Sha1.. */
1498 the_hash_algo->init_fn(&c);
@@ -1758,7 +1758,7 @@ int force_object_loose(const struct object_id *oid, time_t mtime)
1758 buf = read_object(oid->hash, &type, &len);
1759 if (!buf)
1760 return error(_("cannot read sha1_file for %s"), oid_to_hex(oid));
1761 - hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %lu", type_name(type), len) + 1;
1761 + hdrlen = xsnprintf(hdr, sizeof(hdr), "%s %"PRIuMAX , type_name(type), (uintmax_t)len) + 1;
1762 ret = write_loose_object(oid, hdr, hdrlen, buf, len, mtime);
1763 free(buf);
1764