{"thread":{"id":"49787","subject":"[PATCH v1 1/1] Upcast size_t variables to uintmax_t when printing","startedAt":"2018-11-09T14:46:44Z","lastAt":"2018-11-09T19:04:09Z","messageCount":2,"participants":["tboegi@web.de","Eric Sunshine"],"isPatch":true,"patchVersion":1,"patchTotal":1},"messages":[{"id":"362829","messageId":"20181109144638.5330-1-tboegi@web.de","threadId":"49787","inReplyTo":"20181031105947.27562-1-tboegi@web.de","subject":"[PATCH v1 1/1] Upcast size_t variables to uintmax_t when printing","fromName":"","fromEmail":"tboegi@web.de","sentAt":"2018-11-09T14:46:38Z","receivedAt":"2018-11-09T14:46:44Z","isPatch":true,"sender":{"key":"tboegi@web.de","avatar":"https://avatars.githubusercontent.com/u/7138363?v=4"},"body":"From: Torsten Bögershausen <tboegi@web.de>\n\nWhen printing variables which contains a size, today \"unsigned long\"\nis used at many places.\nIn order to be able to change the type from \"unsigned long\" into size_t\nsome day the future, we need to have a way to print 64 bit variables\non a system that has \"unsigned long\" defined to be 32 bit, link Win64.\n\nUpcast all those variables into uintmax_t before they are printed.\nThis is to prepare for a bigger change, when \"unsligned long\"\nwill be converted into size_t for variables which may be > 4Gib.\n\nSigned-off-by: Torsten Bögershausen <tboegi@web.de>\n---\n archive-tar.c          |  2 +-\n builtin/cat-file.c     |  4 ++--\n builtin/fast-export.c  |  2 +-\n builtin/index-pack.c   |  9 +++++----\n builtin/ls-tree.c      |  2 +-\n builtin/pack-objects.c | 12 ++++++------\n diff.c                 |  2 +-\n fast-import.c          |  4 ++--\n http-push.c            |  2 +-\n ref-filter.c           |  2 +-\n sha1-file.c            |  6 +++---\n 11 files changed, 24 insertions(+), 23 deletions(-)\n\ndiff --git a/archive-tar.c b/archive-tar.c\nindex 7a535cba24..a58e1a8ebf 100644\n--- a/archive-tar.c\n+++ b/archive-tar.c\n@@ -202,7 +202,7 @@ static void prepare_header(struct archiver_args *args,\n \t\t\t   unsigned int mode, unsigned long size)\n {\n \txsnprintf(header->mode, sizeof(header->mode), \"%07o\", mode & 07777);\n-\txsnprintf(header->size, sizeof(header->size), \"%011lo\", S_ISREG(mode) ? size : 0);\n+\txsnprintf(header->size, sizeof(header->size), \"%011\"PRIoMAX , S_ISREG(mode) ? (uintmax_t)size : (uintmax_t)0);\n \txsnprintf(header->mtime, sizeof(header->mtime), \"%011lo\", (unsigned long) args->time);\n \n \txsnprintf(header->uid, sizeof(header->uid), \"%07o\", 0);\ndiff --git a/builtin/cat-file.c b/builtin/cat-file.c\nindex 8d97c84725..05decee33f 100644\n--- a/builtin/cat-file.c\n+++ b/builtin/cat-file.c\n@@ -92,7 +92,7 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,\n \t\toi.sizep = &size;\n \t\tif (oid_object_info_extended(the_repository, &oid, &oi, flags) < 0)\n \t\t\tdie(\"git cat-file: could not get object info\");\n-\t\tprintf(\"%lu\\n\", size);\n+\t\tprintf(\"%\"PRIuMAX\"\\n\", (uintmax_t)size);\n \t\treturn 0;\n \n \tcase 'e':\n@@ -238,7 +238,7 @@ static void expand_atom(struct strbuf *sb, const char *atom, int len,\n \t\tif (data->mark_query)\n \t\t\tdata->info.sizep = &data->size;\n \t\telse\n-\t\t\tstrbuf_addf(sb, \"%lu\", data->size);\n+\t\t\tstrbuf_addf(sb, \"%\"PRIuMAX , (uintmax_t)data->size);\n \t} else if (is_atom(\"objectsize:disk\", atom, len)) {\n \t\tif (data->mark_query)\n \t\t\tdata->info.disk_sizep = &data->disk_size;\ndiff --git a/builtin/fast-export.c b/builtin/fast-export.c\nindex 456797c12a..5790f0d554 100644\n--- a/builtin/fast-export.c\n+++ b/builtin/fast-export.c\n@@ -253,7 +253,7 @@ static void export_blob(const struct object_id *oid)\n \n \tmark_next_object(object);\n \n-\tprintf(\"blob\\nmark :%\"PRIu32\"\\ndata %lu\\n\", last_idnum, size);\n+\tprintf(\"blob\\nmark :%\"PRIu32\"\\ndata %\"PRIuMAX\"\\n\", last_idnum, (uintmax_t)size);\n \tif (size && fwrite(buf, size, 1, stdout) != 1)\n \t\tdie_errno(\"could not write blob '%s'\", oid_to_hex(oid));\n \tprintf(\"\\n\");\ndiff --git a/builtin/index-pack.c b/builtin/index-pack.c\nindex 2004e25da2..2a8ada432b 100644\n--- a/builtin/index-pack.c\n+++ b/builtin/index-pack.c\n@@ -450,7 +450,8 @@ static void *unpack_entry_data(off_t offset, unsigned long size,\n \tint hdrlen;\n \n \tif (!is_delta_type(type)) {\n-\t\thdrlen = xsnprintf(hdr, sizeof(hdr), \"%s %lu\", type_name(type), size) + 1;\n+\t\thdrlen = xsnprintf(hdr, sizeof(hdr), \"%s %\"PRIuMAX,\n+\t\t\t\t   type_name(type),(uintmax_t)size) + 1;\n \t\tthe_hash_algo->init_fn(&c);\n \t\tthe_hash_algo->update_fn(&c, hdr, hdrlen);\n \t} else\n@@ -1628,10 +1629,10 @@ static void show_pack_info(int stat_only)\n \t\t\tchain_histogram[obj_stat[i].delta_depth - 1]++;\n \t\tif (stat_only)\n \t\t\tcontinue;\n-\t\tprintf(\"%s %-6s %lu %lu %\"PRIuMAX,\n+\t\tprintf(\"%s %-6s %\"PRIuMAX\" %\"PRIuMAX\" %\"PRIuMAX,\n \t\t       oid_to_hex(&obj->idx.oid),\n-\t\t       type_name(obj->real_type), obj->size,\n-\t\t       (unsigned long)(obj[1].idx.offset - obj->idx.offset),\n+\t\t       type_name(obj->real_type), (uintmax_t)obj->size,\n+\t\t       (uintmax_t)(obj[1].idx.offset - obj->idx.offset),\n \t\t       (uintmax_t)obj->idx.offset);\n \t\tif (is_delta_type(obj->type)) {\n \t\t\tstruct object_entry *bobj = &objects[obj_stat[i].base_object_no];\ndiff --git a/builtin/ls-tree.c b/builtin/ls-tree.c\nindex fe3b952cb3..7d581d6463 100644\n--- a/builtin/ls-tree.c\n+++ b/builtin/ls-tree.c\n@@ -100,7 +100,7 @@ static int show_tree(const struct object_id *oid, struct strbuf *base,\n \t\t\t\t\t\t  \"BAD\");\n \t\t\t\telse\n \t\t\t\t\txsnprintf(size_text, sizeof(size_text),\n-\t\t\t\t\t\t  \"%lu\", size);\n+\t\t\t\t\t\t  \"%\"PRIuMAX, (uintmax_t)size);\n \t\t\t} else\n \t\t\t\txsnprintf(size_text, sizeof(size_text), \"-\");\n \t\t\tprintf(\"%06o %s %s %7s\\t\", mode, type,\ndiff --git a/builtin/pack-objects.c b/builtin/pack-objects.c\nindex e50c6cd1ff..32e2c6b186 100644\n--- a/builtin/pack-objects.c\n+++ b/builtin/pack-objects.c\n@@ -2095,9 +2095,9 @@ static int try_delta(struct unpacked *trg, struct unpacked *src,\n \t\t\tdie(_(\"object %s cannot be read\"),\n \t\t\t    oid_to_hex(&trg_entry->idx.oid));\n \t\tif (sz != trg_size)\n-\t\t\tdie(_(\"object %s inconsistent object length (%lu vs %lu)\"),\n-\t\t\t    oid_to_hex(&trg_entry->idx.oid), sz,\n-\t\t\t    trg_size);\n+\t\t\tdie(_(\"object %s inconsistent object length (%\"PRIuMAX\" vs %\"PRIuMAX\")\"),\n+\t\t\t    oid_to_hex(&trg_entry->idx.oid), (uintmax_t)sz,\n+\t\t\t    (uintmax_t)trg_size);\n \t\t*mem_usage += sz;\n \t}\n \tif (!src->data) {\n@@ -2122,9 +2122,9 @@ static int try_delta(struct unpacked *trg, struct unpacked *src,\n \t\t\t    oid_to_hex(&src_entry->idx.oid));\n \t\t}\n \t\tif (sz != src_size)\n-\t\t\tdie(_(\"object %s inconsistent object length (%lu vs %lu)\"),\n-\t\t\t    oid_to_hex(&src_entry->idx.oid), sz,\n-\t\t\t    src_size);\n+\t\t\tdie(_(\"object %s inconsistent object length (%\"PRIuMAX\" vs %\"PRIuMAX\")\"),\n+\t\t\t    oid_to_hex(&src_entry->idx.oid), (uintmax_t)sz,\n+\t\t\t    (uintmax_t)src_size);\n \t\t*mem_usage += sz;\n \t}\n \tif (!src->index) {\ndiff --git a/diff.c b/diff.c\nindex 8647db3d30..88aaed7d49 100644\n--- a/diff.c\n+++ b/diff.c\n@@ -3227,7 +3227,7 @@ static void emit_binary_diff_body(struct diff_options *o,\n \t}\n \n \tif (delta && delta_size < deflate_size) {\n-\t\tchar *s = xstrfmt(\"%lu\", orig_size);\n+\t\tchar *s = xstrfmt(\"%\"PRIuMAX , (uintmax_t)orig_size);\n \t\temit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA,\n \t\t\t\t s, strlen(s), 0);\n \t\tfree(s);\ndiff --git a/fast-import.c b/fast-import.c\nindex 95600c78e0..69886687ce 100644\n--- a/fast-import.c\n+++ b/fast-import.c\n@@ -2955,8 +2955,8 @@ static void cat_blob(struct object_entry *oe, struct object_id *oid)\n \t\tdie(\"Object %s is a %s but a blob was expected.\",\n \t\t    oid_to_hex(oid), type_name(type));\n \tstrbuf_reset(&line);\n-\tstrbuf_addf(&line, \"%s %s %lu\\n\", oid_to_hex(oid),\n-\t\t\t\t\t\ttype_name(type), size);\n+\tstrbuf_addf(&line, \"%s %s %\"PRIuMAX\"\\n\", oid_to_hex(oid),\n+\t\t    type_name(type), (uintmax_t)size);\n \tcat_blob_write(line.buf, line.len);\n \tstrbuf_release(&line);\n \tcat_blob_write(buf, size);\ndiff --git a/http-push.c b/http-push.c\nindex d1f52cbdf6..cd48590912 100644\n--- a/http-push.c\n+++ b/http-push.c\n@@ -365,7 +365,7 @@ static void start_put(struct transfer_request *request)\n \tgit_zstream stream;\n \n \tunpacked = read_object_file(&request->obj->oid, &type, &len);\n-\thdrlen = xsnprintf(hdr, sizeof(hdr), \"%s %lu\", type_name(type), len) + 1;\n+\thdrlen = xsnprintf(hdr, sizeof(hdr), \"%s %\"PRIuMAX , type_name(type), (uintmax_t)len) + 1;\n \n \t/* Set it up */\n \tgit_deflate_init(&stream, zlib_compression_level);\ndiff --git a/ref-filter.c b/ref-filter.c\nindex 0c45ed9d94..aaac48ad14 100644\n--- a/ref-filter.c\n+++ b/ref-filter.c\n@@ -878,7 +878,7 @@ static void grab_common_values(struct atom_value *val, int deref, struct expand_\n \t\t\tv->s = xstrdup(type_name(oi->type));\n \t\telse if (!strcmp(name, \"objectsize\")) {\n \t\t\tv->value = oi->size;\n-\t\t\tv->s = xstrfmt(\"%lu\", oi->size);\n+\t\t\tv->s = xstrfmt(\"%\"PRIuMAX , (uintmax_t)oi->size);\n \t\t}\n \t\telse if (deref)\n \t\t\tgrab_objectname(name, &oi->oid, v, &used_atom[i]);\ndiff --git a/sha1-file.c b/sha1-file.c\nindex dd0b6aa873..4a67e0e759 100644\n--- a/sha1-file.c\n+++ b/sha1-file.c\n@@ -833,7 +833,7 @@ int check_object_signature(const struct object_id *oid, void *map,\n \t\treturn -1;\n \n \t/* Generate the header */\n-\thdrlen = xsnprintf(hdr, sizeof(hdr), \"%s %lu\", type_name(obj_type), size) + 1;\n+\thdrlen = xsnprintf(hdr, sizeof(hdr), \"%s %\"PRIuMAX , type_name(obj_type), (uintmax_t)size) + 1;\n \n \t/* Sha1.. */\n \tthe_hash_algo->init_fn(&c);\n@@ -1492,7 +1492,7 @@ static void write_object_file_prepare(const void *buf, unsigned long len,\n \tgit_hash_ctx c;\n \n \t/* Generate the header */\n-\t*hdrlen = xsnprintf(hdr, *hdrlen, \"%s %lu\", type, len)+1;\n+\t*hdrlen = xsnprintf(hdr, *hdrlen, \"%s %\"PRIuMAX , type, (uintmax_t)len)+1;\n \n \t/* Sha1.. */\n \tthe_hash_algo->init_fn(&c);\n@@ -1758,7 +1758,7 @@ int force_object_loose(const struct object_id *oid, time_t mtime)\n \tbuf = read_object(oid->hash, &type, &len);\n \tif (!buf)\n \t\treturn error(_(\"cannot read sha1_file for %s\"), oid_to_hex(oid));\n-\thdrlen = xsnprintf(hdr, sizeof(hdr), \"%s %lu\", type_name(type), len) + 1;\n+\thdrlen = xsnprintf(hdr, sizeof(hdr), \"%s %\"PRIuMAX , type_name(type), (uintmax_t)len) + 1;\n \tret = write_loose_object(oid, hdr, hdrlen, buf, len, mtime);\n \tfree(buf);\n \n-- \n2.19.0.271.gfe8321ec05\n\n"},{"id":"362836","messageId":"CAPig+cQE_2JTctQzCKmhMAqaFKf_MMBdEUS6kU41ZbyvUft0JQ@mail.gmail.com","threadId":"49787","inReplyTo":"20181109144638.5330-1-tboegi@web.de","subject":"Re: [PATCH v1 1/1] Upcast size_t variables to uintmax_t when printing","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2018-11-09T19:03:54Z","receivedAt":"2018-11-09T19:04:09Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Fri, Nov 9, 2018 at 9:46 AM <tboegi@web.de> wrote:\n> When printing variables which contains a size, today \"unsigned long\"\n> is used at many places.\n> In order to be able to change the type from \"unsigned long\" into size_t\n> some day the future, we need to have a way to print 64 bit variables\n\ns/day/& in/\n\n> on a system that has \"unsigned long\" defined to be 32 bit, link Win64.\n\ns/link/like/\n\n> Upcast all those variables into uintmax_t before they are printed.\n> This is to prepare for a bigger change, when \"unsligned long\"\n\ns/unsligned/unsigned/\n\n> will be converted into size_t for variables which may be > 4Gib.\n>\n> Signed-off-by: Torsten Bögershausen <tboegi@web.de>\n"}]}