{"thread":{"id":"46552","subject":"[PATCH V2 1/2] Fix delta integer overflows","startedAt":"2017-08-10T07:15:38Z","lastAt":"2017-08-11T18:43:34Z","messageCount":10,"participants":["Martin Koegler","Johannes Schindelin","Junio C Hamano","Jeff King"],"isPatch":true,"patchVersion":2,"patchTotal":2},"messages":[{"id":"326007","messageId":"1502348462-4992-1-git-send-email-martin@mail.zuhause","threadId":"46552","inReplyTo":null,"subject":"[PATCH V2 1/2] Fix delta integer overflows","fromName":"Martin Koegler","fromEmail":"martin.koegler@chello.at","sentAt":"2017-08-10T07:01:01Z","receivedAt":"2017-08-10T07:15:38Z","isPatch":true,"sender":{"key":"martin.koegler@chello.at","avatar":"https://avatars.githubusercontent.com/u/1859030?v=4"},"body":"From: Martin Koegler <martin.koegler@chello.at>\n\nThe current delta code produces incorrect pack objects for files > 4GB.\n\nSigned-off-by: Martin Koegler <martin.koegler@chello.at>\n---\nFor next.\n\n diff-delta.c | 24 +++++++++++++-----------\n 1 file changed, 13 insertions(+), 11 deletions(-)\n\ndiff --git a/diff-delta.c b/diff-delta.c\nindex 3797ce6..cd238c8 100644\n--- a/diff-delta.c\n+++ b/diff-delta.c\n@@ -319,7 +319,9 @@ create_delta(const struct delta_index *index,\n \t     const void *trg_buf, unsigned long trg_size,\n \t     unsigned long *delta_size, unsigned long max_size)\n {\n-\tunsigned int i, outpos, outsize, moff, msize, val;\n+\tunsigned int i, val;\n+\toff_t outpos, moff;\n+\tsize_t l, outsize, msize;\n \tint inscnt;\n \tconst unsigned char *ref_data, *ref_top, *data, *top;\n \tunsigned char *out;\n@@ -336,20 +338,20 @@ create_delta(const struct delta_index *index,\n \t\treturn NULL;\n \n \t/* store reference buffer size */\n-\ti = index->src_size;\n-\twhile (i >= 0x80) {\n-\t\tout[outpos++] = i | 0x80;\n-\t\ti >>= 7;\n+\tl = index->src_size;\n+\twhile (l >= 0x80) {\n+\t\tout[outpos++] = l | 0x80;\n+\t\tl >>= 7;\n \t}\n-\tout[outpos++] = i;\n+\tout[outpos++] = l;\n \n \t/* store target buffer size */\n-\ti = trg_size;\n-\twhile (i >= 0x80) {\n-\t\tout[outpos++] = i | 0x80;\n-\t\ti >>= 7;\n+\tl = trg_size;\n+\twhile (l >= 0x80) {\n+\t\tout[outpos++] = l | 0x80;\n+\t\tl >>= 7;\n \t}\n-\tout[outpos++] = i;\n+\tout[outpos++] = l;\n \n \tref_data = index->src_buf;\n \tref_top = ref_data + index->src_size;\n-- \n2.1.4\n\n"},{"id":"326008","messageId":"1502348462-4992-2-git-send-email-martin@mail.zuhause","threadId":"46552","inReplyTo":"1502348462-4992-1-git-send-email-martin@mail.zuhause","subject":"[PATCH V2 2/2] Convert size datatype to size_t","fromName":"Martin Koegler","fromEmail":"martin.koegler@chello.at","sentAt":"2017-08-10T07:01:02Z","receivedAt":"2017-08-10T07:15:41Z","isPatch":true,"sender":{"key":"martin.koegler@chello.at","avatar":"https://avatars.githubusercontent.com/u/1859030?v=4"},"body":"From: Martin Koegler <martin.koegler@chello.at>\n\nIt changes the signature of the core object access function\nincluding any other functions to assure a clean compile if\nsizeof(size_t) != sizeof(unsigned long).\n\nSigned-off-by: Martin Koegler <martin.koegler@chello.at>\n---\nFor next. As this touches core functions, it will likely produce\nconflicts with other changes. Please provide the commit you want\nto rebase the patch on and I'll produce a V3.\n\nIncludes changes from Johannes Schindelin.\n\n apply.c                  |  6 +++---\n archive-tar.c            |  4 ++--\n archive-zip.c            |  2 +-\n archive.c                |  2 +-\n archive.h                |  2 +-\n blame.c                  |  4 ++--\n blame.h                  |  2 +-\n builtin/cat-file.c       | 14 +++++++-------\n builtin/difftool.c       |  2 +-\n builtin/fast-export.c    |  8 ++++----\n builtin/fmt-merge-msg.c  |  2 +-\n builtin/fsck.c           |  2 +-\n builtin/grep.c           |  8 ++++----\n builtin/index-pack.c     | 16 ++++++++--------\n builtin/log.c            |  4 ++--\n builtin/ls-tree.c        |  4 ++--\n builtin/merge-tree.c     |  6 +++---\n builtin/mktag.c          |  2 +-\n builtin/notes.c          |  6 +++---\n builtin/pack-objects.c   | 24 ++++++++++++-----------\n builtin/reflog.c         |  2 +-\n builtin/tag.c            |  4 ++--\n builtin/unpack-file.c    |  2 +-\n builtin/unpack-objects.c | 14 +++++++-------\n builtin/verify-commit.c  |  2 +-\n bundle.c                 |  2 +-\n cache.h                  | 22 ++++++++++-----------\n combine-diff.c           |  9 +++++----\n commit.c                 |  6 +++---\n config.c                 |  2 +-\n delta.h                  | 26 ++++++++++++-------------\n diff-delta.c             | 16 ++++++++--------\n diff.c                   | 18 ++++++++---------\n diff.h                   |  2 +-\n diffcore.h               |  2 +-\n dir.c                    |  2 +-\n entry.c                  |  4 ++--\n fast-import.c            | 24 +++++++++++------------\n fsck.c                   |  2 +-\n grep.h                   |  2 +-\n http-push.c              |  5 +++--\n mailmap.c                |  2 +-\n match-trees.c            |  4 ++--\n merge-blobs.c            |  6 +++---\n merge-blobs.h            |  2 +-\n merge-recursive.c        |  4 ++--\n notes-cache.c            |  2 +-\n notes-merge.c            |  2 +-\n notes.c                  |  6 +++---\n object.c                 |  2 +-\n pack-check.c             |  2 +-\n pack-objects.h           |  6 +++---\n patch-delta.c            | 11 ++++++-----\n read-cache.c             |  4 ++--\n ref-filter.c             |  4 ++--\n remote-testsvn.c         |  4 ++--\n rerere.c                 |  2 +-\n sha1_file.c              | 50 +++++++++++++++++++++++++-----------------------\n streaming.c              |  8 ++++----\n streaming.h              |  2 +-\n submodule-config.c       |  2 +-\n t/helper/test-delta.c    |  2 +-\n tag.c                    |  4 ++--\n tree-walk.c              |  8 ++++----\n tree.c                   |  2 +-\n xdiff-interface.c        |  2 +-\n 66 files changed, 219 insertions(+), 212 deletions(-)\n\ndiff --git a/apply.c b/apply.c\nindex 41ee63e..af9ffee 100644\n--- a/apply.c\n+++ b/apply.c\n@@ -3082,7 +3082,7 @@ static int apply_binary_fragment(struct apply_state *state,\n \t\t\t\t struct patch *patch)\n {\n \tstruct fragment *fragment = patch->fragments;\n-\tunsigned long len;\n+\tsize_t len;\n \tvoid *dst;\n \n \tif (!fragment)\n@@ -3171,7 +3171,7 @@ static int apply_binary(struct apply_state *state,\n \tif (has_sha1_file(oid.hash)) {\n \t\t/* We already have the postimage */\n \t\tenum object_type type;\n-\t\tunsigned long size;\n+\t\tsize_t size;\n \t\tchar *result;\n \n \t\tresult = read_sha1_file(oid.hash, &type, &size);\n@@ -3233,7 +3233,7 @@ static int read_blob_object(struct strbuf *buf, const struct object_id *oid, uns\n \t\tstrbuf_addf(buf, \"Subproject commit %s\\n\", oid_to_hex(oid));\n \t} else {\n \t\tenum object_type type;\n-\t\tunsigned long sz;\n+\t\tsize_t sz;\n \t\tchar *result;\n \n \t\tresult = read_sha1_file(oid->hash, &type, &sz);\ndiff --git a/archive-tar.c b/archive-tar.c\nindex c6ed96e..719673d 100644\n--- a/archive-tar.c\n+++ b/archive-tar.c\n@@ -115,7 +115,7 @@ static int stream_blocked(const unsigned char *sha1)\n {\n \tstruct git_istream *st;\n \tenum object_type type;\n-\tunsigned long sz;\n+\tsize_t sz;\n \tchar buf[BLOCKSIZE];\n \tssize_t readlen;\n \n@@ -240,7 +240,7 @@ static int write_tar_entry(struct archiver_args *args,\n \tstruct ustar_header header;\n \tstruct strbuf ext_header = STRBUF_INIT;\n \tunsigned int old_mode = mode;\n-\tunsigned long size, size_in_header;\n+\tsize_t size, size_in_header;\n \tvoid *buffer;\n \tint err = 0;\n \ndiff --git a/archive-zip.c b/archive-zip.c\nindex e8913e5..4492d64 100644\n--- a/archive-zip.c\n+++ b/archive-zip.c\n@@ -295,7 +295,7 @@ static int write_zip_entry(struct archiver_args *args,\n \tvoid *buffer;\n \tstruct git_istream *stream = NULL;\n \tunsigned long flags = 0;\n-\tunsigned long size;\n+\tsize_t size;\n \tint is_binary = -1;\n \tconst char *path_without_prefix = path + args->baselen;\n \tunsigned int creator_version = 0;\ndiff --git a/archive.c b/archive.c\nindex 557dd2d..92f3960 100644\n--- a/archive.c\n+++ b/archive.c\n@@ -66,7 +66,7 @@ static void format_subst(const struct commit *commit,\n void *sha1_file_to_archive(const struct archiver_args *args,\n \t\t\t   const char *path, const unsigned char *sha1,\n \t\t\t   unsigned int mode, enum object_type *type,\n-\t\t\t   unsigned long *sizep)\n+\t\t\t   size_t *sizep)\n {\n \tvoid *buffer;\n \tconst struct commit *commit = args->convert ? args->commit : NULL;\ndiff --git a/archive.h b/archive.h\nindex 62d1d82..dd831a3 100644\n--- a/archive.h\n+++ b/archive.h\n@@ -42,6 +42,6 @@ const char *archive_format_from_filename(const char *filename);\n extern void *sha1_file_to_archive(const struct archiver_args *args,\n \t\t\t\t  const char *path, const unsigned char *sha1,\n \t\t\t\t  unsigned int mode, enum object_type *type,\n-\t\t\t\t  unsigned long *sizep);\n+\t\t\t\t  size_t *sizep);\n \n #endif\t/* ARCHIVE_H */\ndiff --git a/blame.c b/blame.c\nindex f575e9c..739a280 100644\n--- a/blame.c\n+++ b/blame.c\n@@ -193,7 +193,7 @@ static struct commit *fake_working_tree_commit(struct diff_options *opt,\n \t\tstruct stat st;\n \t\tconst char *read_from;\n \t\tchar *buf_ptr;\n-\t\tunsigned long buf_len;\n+\t\tsize_t buf_len;\n \n \t\tif (contents_from) {\n \t\t\tif (stat(contents_from, &st) < 0)\n@@ -290,7 +290,7 @@ static void fill_origin_blob(struct diff_options *opt,\n {\n \tif (!o->file.ptr) {\n \t\tenum object_type type;\n-\t\tunsigned long file_size;\n+\t\tsize_t file_size;\n \n \t\t(*num_read_blob)++;\n \t\tif (DIFF_OPT_TST(opt, ALLOW_TEXTCONV) &&\ndiff --git a/blame.h b/blame.h\nindex a6c915c..9a0ed1c 100644\n--- a/blame.h\n+++ b/blame.h\n@@ -111,7 +111,7 @@ struct blame_scoreboard {\n \t * indexed with scoreboard.lineno[blame_entry.lno].\n \t */\n \tconst char *final_buf;\n-\tunsigned long final_buf_size;\n+\tsize_t final_buf_size;\n \n \t/* linked list of blames */\n \tstruct blame_entry *ent;\ndiff --git a/builtin/cat-file.c b/builtin/cat-file.c\nindex 62c8cf0..045cab5 100644\n--- a/builtin/cat-file.c\n+++ b/builtin/cat-file.c\n@@ -27,7 +27,7 @@ static const char *force_path;\n \n static int filter_object(const char *path, unsigned mode,\n \t\t\t const struct object_id *oid,\n-\t\t\t char **buf, unsigned long *size)\n+\t\t\t char **buf, size_t *size)\n {\n \tenum object_type type;\n \n@@ -53,7 +53,7 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,\n \tstruct object_id oid;\n \tenum object_type type;\n \tchar *buf;\n-\tunsigned long size;\n+\tsize_t size;\n \tstruct object_context obj_context;\n \tstruct object_info oi = OBJECT_INFO_INIT;\n \tstruct strbuf sb = STRBUF_INIT;\n@@ -89,7 +89,7 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,\n \t\toi.sizep = &size;\n \t\tif (sha1_object_info_extended(oid.hash, &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@@ -176,7 +176,7 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name,\n struct expand_data {\n \tstruct object_id oid;\n \tenum object_type type;\n-\tunsigned long size;\n+\tsize_t size;\n \toff_t disk_size;\n \tconst char *rest;\n \tstruct object_id delta_base_oid;\n@@ -232,7 +232,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;\n@@ -288,7 +288,7 @@ static void print_object_or_die(struct batch_options *opt, struct expand_data *d\n \t\t\tfflush(stdout);\n \t\tif (opt->cmdmode) {\n \t\t\tchar *contents;\n-\t\t\tunsigned long size;\n+\t\t\tsize_t size;\n \n \t\t\tif (!data->rest)\n \t\t\t\tdie(\"missing path for '%s'\", oid_to_hex(oid));\n@@ -316,7 +316,7 @@ static void print_object_or_die(struct batch_options *opt, struct expand_data *d\n \t}\n \telse {\n \t\tenum object_type type;\n-\t\tunsigned long size;\n+\t\tsize_t size;\n \t\tvoid *contents;\n \n \t\tcontents = read_sha1_file(oid->hash, &type, &size);\ndiff --git a/builtin/difftool.c b/builtin/difftool.c\nindex 8864d84..525d4d9 100644\n--- a/builtin/difftool.c\n+++ b/builtin/difftool.c\n@@ -305,7 +305,7 @@ static char *get_symlink(const struct object_id *oid, const char *path)\n \t\tdata = strbuf_detach(&link, NULL);\n \t} else {\n \t\tenum object_type type;\n-\t\tunsigned long size;\n+\t\tsize_t size;\n \t\tdata = read_sha1_file(oid->hash, &type, &size);\n \t\tif (!data)\n \t\t\tdie(_(\"could not read object %s for symlink %s\"),\ndiff --git a/builtin/fast-export.c b/builtin/fast-export.c\nindex d412c0a..9ef2fc1 100644\n--- a/builtin/fast-export.c\n+++ b/builtin/fast-export.c\n@@ -205,7 +205,7 @@ static void show_progress(void)\n  * There's no need to cache this result with anonymize_mem, since\n  * we already handle blob content caching with marks.\n  */\n-static char *anonymize_blob(unsigned long *size)\n+static char *anonymize_blob(size_t *size)\n {\n \tstatic int counter;\n \tstruct strbuf out = STRBUF_INIT;\n@@ -216,7 +216,7 @@ static char *anonymize_blob(unsigned long *size)\n \n static void export_blob(const struct object_id *oid)\n {\n-\tunsigned long size;\n+\tsize_t size;\n \tenum object_type type;\n \tchar *buf;\n \tstruct object *object;\n@@ -250,7 +250,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\");\n@@ -644,7 +644,7 @@ static void handle_tail(struct object_array *commits, struct rev_info *revs)\n \n static void handle_tag(const char *name, struct tag *tag)\n {\n-\tunsigned long size;\n+\tsize_t size;\n \tenum object_type type;\n \tchar *buf;\n \tconst char *tagger, *tagger_end, *message;\ndiff --git a/builtin/fmt-merge-msg.c b/builtin/fmt-merge-msg.c\nindex e99b5dd..61ab796 100644\n--- a/builtin/fmt-merge-msg.c\n+++ b/builtin/fmt-merge-msg.c\n@@ -486,7 +486,7 @@ static void fmt_merge_msg_sigs(struct strbuf *out)\n \tfor (i = 0; i < origins.nr; i++) {\n \t\tunsigned char *sha1 = origins.items[i].util;\n \t\tenum object_type type;\n-\t\tunsigned long size, len;\n+\t\tsize_t size, len;\n \t\tchar *buf = read_sha1_file(sha1, &type, &size);\n \t\tstruct strbuf sig = STRBUF_INIT;\n \ndiff --git a/builtin/fsck.c b/builtin/fsck.c\nindex a92f448..635902c 100644\n--- a/builtin/fsck.c\n+++ b/builtin/fsck.c\n@@ -489,7 +489,7 @@ static struct object *parse_loose_object(const struct object_id *oid,\n \tstruct object *obj;\n \tvoid *contents;\n \tenum object_type type;\n-\tunsigned long size;\n+\tsize_t size;\n \tint eaten;\n \n \tif (read_loose_object(path, oid->hash, &type, &size, &contents) < 0)\ndiff --git a/builtin/grep.c b/builtin/grep.c\nindex a70d8e2..b032d24 100644\n--- a/builtin/grep.c\n+++ b/builtin/grep.c\n@@ -302,7 +302,7 @@ static int grep_cmd_config(const char *var, const char *value, void *cb)\n \treturn st;\n }\n \n-static void *lock_and_read_oid_file(const struct object_id *oid, enum object_type *type, unsigned long *size)\n+static void *lock_and_read_oid_file(const struct object_id *oid, enum object_type *type, size_t *size)\n {\n \tvoid *data;\n \n@@ -437,7 +437,7 @@ static int grep_submodule(struct grep_opt *opt, struct repository *superproject,\n \t\tstruct object *object;\n \t\tstruct tree_desc tree;\n \t\tvoid *data;\n-\t\tunsigned long size;\n+\t\tsize_t size;\n \t\tstruct strbuf base = STRBUF_INIT;\n \n \t\tobject = parse_object_or_die(oid, oid_to_hex(oid));\n@@ -564,7 +564,7 @@ static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,\n \t\t\tenum object_type type;\n \t\t\tstruct tree_desc sub;\n \t\t\tvoid *data;\n-\t\t\tunsigned long size;\n+\t\t\tsize_t size;\n \n \t\t\tdata = lock_and_read_oid_file(entry.oid, &type, &size);\n \t\t\tif (!data)\n@@ -600,7 +600,7 @@ static int grep_object(struct grep_opt *opt, const struct pathspec *pathspec,\n \tif (obj->type == OBJ_COMMIT || obj->type == OBJ_TREE) {\n \t\tstruct tree_desc tree;\n \t\tvoid *data;\n-\t\tunsigned long size;\n+\t\tsize_t size;\n \t\tstruct strbuf base;\n \t\tint hit, len;\n \ndiff --git a/builtin/index-pack.c b/builtin/index-pack.c\nindex 26828c1..7f3ccd0 100644\n--- a/builtin/index-pack.c\n+++ b/builtin/index-pack.c\n@@ -18,8 +18,8 @@ static const char index_pack_usage[] =\n \n struct object_entry {\n \tstruct pack_idx_entry idx;\n-\tunsigned long size;\n-\tunsigned char hdr_size;\n+\tsize_t size;\n+\tsize_t hdr_size;\n \tsigned char type;\n \tsigned char real_type;\n };\n@@ -34,7 +34,7 @@ struct base_data {\n \tstruct base_data *child;\n \tstruct object_entry *obj;\n \tvoid *data;\n-\tunsigned long size;\n+\tsize_t size;\n \tint ref_first, ref_last;\n \tint ofs_first, ofs_last;\n };\n@@ -219,7 +219,7 @@ static unsigned check_object(struct object *obj)\n \t\treturn 0;\n \n \tif (!(obj->flags & FLAG_CHECKED)) {\n-\t\tunsigned long size;\n+\t\tsize_t size;\n \t\tint type = sha1_object_info(obj->oid.hash, &size);\n \t\tif (type <= 0)\n \t\t\tdie(_(\"did not receive expected object %s\"),\n@@ -763,7 +763,7 @@ static int check_collison(struct object_entry *entry)\n {\n \tstruct compare_data data;\n \tenum object_type type;\n-\tunsigned long size;\n+\tsize_t size;\n \n \tif (entry->size <= big_file_threshold || entry->type != OBJ_BLOB)\n \t\treturn -1;\n@@ -807,7 +807,7 @@ static void sha1_object(const void *data, struct object_entry *obj_entry,\n \tif (collision_test_needed) {\n \t\tvoid *has_data;\n \t\tenum object_type has_type;\n-\t\tunsigned long has_size;\n+\t\tsize_t has_size;\n \t\tread_lock();\n \t\thas_type = sha1_object_info(oid->hash, &has_size);\n \t\tif (has_type < 0)\n@@ -1585,9 +1585,9 @@ 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 \" %lu %\" PRIuMAX,\n \t\t       oid_to_hex(&obj->idx.oid),\n-\t\t       typename(obj->real_type), obj->size,\n+\t\t       typename(obj->real_type), (uintmax_t)obj->size,\n \t\t       (unsigned long)(obj[1].idx.offset - obj->idx.offset),\n \t\t       (uintmax_t)obj->idx.offset);\n \t\tif (is_delta_type(obj->type)) {\ndiff --git a/builtin/log.c b/builtin/log.c\nindex 725c7b8..2330822 100644\n--- a/builtin/log.c\n+++ b/builtin/log.c\n@@ -480,7 +480,7 @@ static int show_blob_object(const struct object_id *oid, struct rev_info *rev, c\n \tstruct object_id oidc;\n \tstruct object_context obj_context;\n \tchar *buf;\n-\tunsigned long size;\n+\tsize_t size;\n \n \tfflush(rev->diffopt.file);\n \tif (!DIFF_OPT_TOUCHED(&rev->diffopt, ALLOW_TEXTCONV) ||\n@@ -506,7 +506,7 @@ static int show_blob_object(const struct object_id *oid, struct rev_info *rev, c\n \n static int show_tag_object(const struct object_id *oid, struct rev_info *rev)\n {\n-\tunsigned long size;\n+\tsize_t size;\n \tenum object_type type;\n \tchar *buf = read_sha1_file(oid->hash, &type, &size);\n \tint offset = 0;\ndiff --git a/builtin/ls-tree.c b/builtin/ls-tree.c\nindex ef96540..b09d9cb 100644\n--- a/builtin/ls-tree.c\n+++ b/builtin/ls-tree.c\n@@ -93,13 +93,13 @@ static int show_tree(const unsigned char *sha1, struct strbuf *base,\n \t\tif (ls_options & LS_SHOW_SIZE) {\n \t\t\tchar size_text[24];\n \t\t\tif (!strcmp(type, blob_type)) {\n-\t\t\t\tunsigned long size;\n+\t\t\t\tsize_t size;\n \t\t\t\tif (sha1_object_info(sha1, &size) == OBJ_BAD)\n \t\t\t\t\txsnprintf(size_text, sizeof(size_text),\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/merge-tree.c b/builtin/merge-tree.c\nindex f12da29..2edbad2 100644\n--- a/builtin/merge-tree.c\n+++ b/builtin/merge-tree.c\n@@ -53,7 +53,7 @@ static const char *explanation(struct merge_list *entry)\n \treturn \"removed in remote\";\n }\n \n-static void *result(struct merge_list *entry, unsigned long *size)\n+static void *result(struct merge_list *entry, size_t *size)\n {\n \tenum object_type type;\n \tstruct blob *base, *our, *their;\n@@ -77,7 +77,7 @@ static void *result(struct merge_list *entry, unsigned long *size)\n \treturn merge_blobs(path, base, our, their, size);\n }\n \n-static void *origin(struct merge_list *entry, unsigned long *size)\n+static void *origin(struct merge_list *entry, size_t *size)\n {\n \tenum object_type type;\n \twhile (entry) {\n@@ -98,7 +98,7 @@ static int show_outf(void *priv_, mmbuffer_t *mb, int nbuf)\n \n static void show_diff(struct merge_list *entry)\n {\n-\tunsigned long size;\n+\tsize_t size;\n \tmmfile_t src, dst;\n \txpparam_t xpp;\n \txdemitconf_t xecfg;\ndiff --git a/builtin/mktag.c b/builtin/mktag.c\nindex 031b750..0663106 100644\n--- a/builtin/mktag.c\n+++ b/builtin/mktag.c\n@@ -22,7 +22,7 @@ static int verify_object(const unsigned char *sha1, const char *expected_type)\n {\n \tint ret = -1;\n \tenum object_type type;\n-\tunsigned long size;\n+\tsize_t size;\n \tvoid *buffer = read_sha1_file(sha1, &type, &size);\n \tconst unsigned char *repl = lookup_replace_object(sha1);\n \ndiff --git a/builtin/notes.c b/builtin/notes.c\nindex 4303848..7932a8e 100644\n--- a/builtin/notes.c\n+++ b/builtin/notes.c\n@@ -120,7 +120,7 @@ static int list_each_note(const struct object_id *object_oid,\n \n static void copy_obj_to_fd(int fd, const unsigned char *sha1)\n {\n-\tunsigned long size;\n+\tsize_t size;\n \tenum object_type type;\n \tchar *buf = read_sha1_file(sha1, &type, &size);\n \tif (buf) {\n@@ -246,7 +246,7 @@ static int parse_reuse_arg(const struct option *opt, const char *arg, int unset)\n \tchar *buf;\n \tstruct object_id object;\n \tenum object_type type;\n-\tunsigned long len;\n+\tsize_t len;\n \n \tif (d->buf.len)\n \t\tstrbuf_addch(&d->buf, '\\n');\n@@ -605,7 +605,7 @@ static int append_edit(int argc, const char **argv, const char *prefix)\n \n \tif (note && !edit) {\n \t\t/* Append buf to previous note contents */\n-\t\tunsigned long size;\n+\t\tsize_t size;\n \t\tenum object_type type;\n \t\tchar *prev_buf = read_sha1_file(note->hash, &type, &size);\n \ndiff --git a/builtin/pack-objects.c b/builtin/pack-objects.c\nindex c753e92..d94fd17 100644\n--- a/builtin/pack-objects.c\n+++ b/builtin/pack-objects.c\n@@ -103,7 +103,7 @@ static void index_commit_for_bitmap(struct commit *commit)\n \n static void *get_delta(struct object_entry *entry)\n {\n-\tunsigned long size, base_size, delta_size;\n+\tsize_t size, base_size, delta_size;\n \tvoid *buf, *base_buf, *delta_buf;\n \tenum object_type type;\n \n@@ -241,7 +241,7 @@ static void copy_pack_data(struct sha1file *f,\n static unsigned long write_no_reuse_object(struct sha1file *f, struct object_entry *entry,\n \t\t\t\t\t   unsigned long limit, int usable_delta)\n {\n-\tunsigned long size, datalen;\n+\tsize_t size, datalen;\n \tunsigned char header[MAX_PACK_OBJECT_HEADER],\n \t\t      dheader[MAX_PACK_OBJECT_HEADER];\n \tunsigned hdrlen;\n@@ -1147,7 +1147,7 @@ static struct pbase_tree_cache *pbase_tree_get(const unsigned char *sha1)\n {\n \tstruct pbase_tree_cache *ent, *nent;\n \tvoid *data;\n-\tunsigned long size;\n+\tsize_t size;\n \tenum object_type type;\n \tint neigh;\n \tint my_ix = pbase_tree_cache_ix(sha1);\n@@ -1330,7 +1330,7 @@ static void add_preferred_base(unsigned char *sha1)\n {\n \tstruct pbase_tree *it;\n \tvoid *data;\n-\tunsigned long size;\n+\tsize_t size;\n \tunsigned char tree_sha1[20];\n \n \tif (window <= num_preferred_base++)\n@@ -1805,7 +1805,7 @@ static int try_delta(struct unpacked *trg, struct unpacked *src,\n {\n \tstruct object_entry *trg_entry = trg->entry;\n \tstruct object_entry *src_entry = src->entry;\n-\tunsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;\n+\tsize_t trg_size, src_size, delta_size, sizediff, max_size, sz;\n \tunsigned ref_depth;\n \tenum object_type type;\n \tvoid *delta_buf;\n@@ -1863,9 +1863,10 @@ 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\n+\t\t\t    \" 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@@ -1891,9 +1892,10 @@ 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\n+\t\t\t    \" 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/builtin/reflog.c b/builtin/reflog.c\nindex e237d92..7f0382d 100644\n--- a/builtin/reflog.c\n+++ b/builtin/reflog.c\n@@ -73,7 +73,7 @@ static int tree_is_complete(const struct object_id *oid)\n \n \tif (!tree->buffer) {\n \t\tenum object_type type;\n-\t\tunsigned long size;\n+\t\tsize_t size;\n \t\tvoid *data = read_sha1_file(oid->hash, &type, &size);\n \t\tif (!data) {\n \t\t\ttree->object.flags |= INCOMPLETE;\ndiff --git a/builtin/tag.c b/builtin/tag.c\nindex c627794..864461b 100644\n--- a/builtin/tag.c\n+++ b/builtin/tag.c\n@@ -163,7 +163,7 @@ static int git_tag_config(const char *var, const char *value, void *cb)\n \n static void write_tag_body(int fd, const struct object_id *oid)\n {\n-\tunsigned long size;\n+\tsize_t size;\n \tenum object_type type;\n \tchar *buf, *sp;\n \n@@ -280,7 +280,7 @@ static void create_reflog_msg(const struct object_id *oid, struct strbuf *sb)\n \tenum object_type type;\n \tstruct commit *c;\n \tchar *buf;\n-\tunsigned long size;\n+\tsize_t size;\n \tint subject_len = 0;\n \tconst char *subject_start;\n \ndiff --git a/builtin/unpack-file.c b/builtin/unpack-file.c\nindex 281ca1d..16c1431 100644\n--- a/builtin/unpack-file.c\n+++ b/builtin/unpack-file.c\n@@ -6,7 +6,7 @@ static char *create_temp_file(struct object_id *oid)\n \tstatic char path[50];\n \tvoid *buf;\n \tenum object_type type;\n-\tunsigned long size;\n+\tsize_t size;\n \tint fd;\n \n \tbuf = read_sha1_file(oid->hash, &type, &size);\ndiff --git a/builtin/unpack-objects.c b/builtin/unpack-objects.c\nindex 689a29f..001dd4b 100644\n--- a/builtin/unpack-objects.c\n+++ b/builtin/unpack-objects.c\n@@ -196,7 +196,7 @@ static int check_object(struct object *obj, int type, void *data, struct fsck_op\n \t\tdie(\"object type mismatch\");\n \n \tif (!(obj->flags & FLAG_OPEN)) {\n-\t\tunsigned long size;\n+\t\tsize_t size;\n \t\tint type = sha1_object_info(obj->oid.hash, &size);\n \t\tif (type != obj->type || type <= 0)\n \t\t\tdie(\"object of unexpected type\");\n@@ -271,11 +271,11 @@ static void write_object(unsigned nr, enum object_type type,\n }\n \n static void resolve_delta(unsigned nr, enum object_type type,\n-\t\t\t  void *base, unsigned long base_size,\n-\t\t\t  void *delta, unsigned long delta_size)\n+\t\t\t  void *base, size_t base_size,\n+\t\t\t  void *delta, size_t delta_size)\n {\n \tvoid *result;\n-\tunsigned long result_size;\n+\tsize_t result_size;\n \n \tresult = patch_delta(base, base_size,\n \t\t\t     delta, delta_size,\n@@ -322,7 +322,7 @@ static void unpack_non_delta_entry(enum object_type type, unsigned long size,\n }\n \n static int resolve_against_held(unsigned nr, const struct object_id *base,\n-\t\t\t\tvoid *delta_data, unsigned long delta_size)\n+\t\t\t\tvoid *delta_data, size_t delta_size)\n {\n \tstruct object *obj;\n \tstruct obj_buffer *obj_buffer;\n@@ -337,11 +337,11 @@ static int resolve_against_held(unsigned nr, const struct object_id *base,\n \treturn 1;\n }\n \n-static void unpack_delta_entry(enum object_type type, unsigned long delta_size,\n+static void unpack_delta_entry(enum object_type type, size_t delta_size,\n \t\t\t       unsigned nr)\n {\n \tvoid *delta_data, *base;\n-\tunsigned long base_size;\n+\tsize_t base_size;\n \tstruct object_id base_oid;\n \n \tif (type == OBJ_REF_DELTA) {\ndiff --git a/builtin/verify-commit.c b/builtin/verify-commit.c\nindex ba38ac9..ea0857a 100644\n--- a/builtin/verify-commit.c\n+++ b/builtin/verify-commit.c\n@@ -38,7 +38,7 @@ static int verify_commit(const char *name, unsigned flags)\n \tenum object_type type;\n \tstruct object_id oid;\n \tchar *buf;\n-\tunsigned long size;\n+\tsize_t size;\n \tint ret;\n \n \tif (get_oid(name, &oid))\ndiff --git a/bundle.c b/bundle.c\nindex d15db03..c49cb13 100644\n--- a/bundle.c\n+++ b/bundle.c\n@@ -209,7 +209,7 @@ int list_bundle_refs(struct bundle_header *header, int argc, const char **argv)\n \n static int is_tag_in_date_range(struct object *tag, struct rev_info *revs)\n {\n-\tunsigned long size;\n+\tsize_t size;\n \tenum object_type type;\n \tchar *buf = NULL, *line, *lineend;\n \ttimestamp_t date;\ndiff --git a/cache.h b/cache.h\nindex a2686c4..26a3eaa 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -1162,8 +1162,8 @@ extern char *xdg_cache_home(const char *filename);\n \n extern void *read_sha1_file_extended(const unsigned char *sha1,\n \t\t\t\t     enum object_type *type,\n-\t\t\t\t     unsigned long *size, int lookup_replace);\n-static inline void *read_sha1_file(const unsigned char *sha1, enum object_type *type, unsigned long *size)\n+\t\t\t\t     size_t *size, int lookup_replace);\n+static inline void *read_sha1_file(const unsigned char *sha1, enum object_type *type, size_t *size)\n {\n \treturn read_sha1_file_extended(sha1, type, size, 1);\n }\n@@ -1188,7 +1188,7 @@ static inline const unsigned char *lookup_replace_object(const unsigned char *sh\n }\n \n /* Read and unpack a sha1 file into memory, write memory to a sha1 file */\n-extern int sha1_object_info(const unsigned char *, unsigned long *);\n+extern int sha1_object_info(const unsigned char *, size_t *);\n extern int hash_sha1_file(const void *buf, unsigned long len, const char *type, unsigned char *sha1);\n extern int write_sha1_file(const void *buf, unsigned long len, const char *type, unsigned char *return_sha1);\n extern int hash_sha1_file_literally(const void *buf, unsigned long len, const char *type, unsigned char *sha1, unsigned flags);\n@@ -1198,12 +1198,12 @@ extern int git_open_cloexec(const char *name, int flags);\n #define git_open(name) git_open_cloexec(name, O_RDONLY)\n extern void *map_sha1_file(const unsigned char *sha1, unsigned long *size);\n extern int unpack_sha1_header(git_zstream *stream, unsigned char *map, unsigned long mapsize, void *buffer, unsigned long bufsiz);\n-extern int parse_sha1_header(const char *hdr, unsigned long *sizep);\n+extern int parse_sha1_header(const char *hdr, size_t *sizep);\n \n /* global flag to enable extra checks when accessing packed objects */\n extern int do_check_packed_object_crc;\n \n-extern int check_sha1_signature(const unsigned char *sha1, void *buf, unsigned long size, const char *type);\n+extern int check_sha1_signature(const unsigned char *sha1, void *buf, size_t size, const char *type);\n \n extern int finalize_object_file(const char *tmpfile, const char *filename);\n \n@@ -1219,7 +1219,7 @@ extern int has_sha1_pack(const unsigned char *sha1);\n int read_loose_object(const char *path,\n \t\t      const unsigned char *expected_sha1,\n \t\t      enum object_type *type,\n-\t\t      unsigned long *size,\n+\t\t      size_t *size,\n \t\t      void **contents);\n \n /*\n@@ -1397,7 +1397,7 @@ extern int cache_name_stage_compare(const char *name1, int len1, int stage1, con\n \n extern void *read_object_with_reference(const unsigned char *sha1,\n \t\t\t\t\tconst char *required_type,\n-\t\t\t\t\tunsigned long *size,\n+\t\t\t\t\tsize_t *size,\n \t\t\t\t\tunsigned char *sha1_ret);\n \n extern struct object *peel_to_type(const char *name, int namelen,\n@@ -1722,10 +1722,10 @@ extern off_t nth_packed_object_offset(const struct packed_git *, uint32_t n);\n extern off_t find_pack_entry_one(const unsigned char *sha1, struct packed_git *);\n \n extern int is_pack_valid(struct packed_git *);\n-extern void *unpack_entry(struct packed_git *, off_t, enum object_type *, unsigned long *);\n-extern unsigned long unpack_object_header_buffer(const unsigned char *buf, unsigned long len, enum object_type *type, unsigned long *sizep);\n+extern void *unpack_entry(struct packed_git *, off_t, enum object_type *, size_t *);\n+extern unsigned long unpack_object_header_buffer(const unsigned char *buf, unsigned long len, enum object_type *type, size_t *sizep);\n extern unsigned long get_size_from_delta(struct packed_git *, struct pack_window **, off_t);\n-extern int unpack_object_header(struct packed_git *, struct pack_window **, off_t *, unsigned long *);\n+extern int unpack_object_header(struct packed_git *, struct pack_window **, off_t *, size_t *);\n \n /*\n  * Iterate over the files in the loose-object parts of the object\n@@ -1791,7 +1791,7 @@ extern int for_each_packed_object(each_packed_object_fn, void *, unsigned flags)\n struct object_info {\n \t/* Request */\n \tenum object_type *typep;\n-\tunsigned long *sizep;\n+\tsize_t *sizep;\n \toff_t *disk_sizep;\n \tunsigned char *delta_base_sha1;\n \tstruct strbuf *typename;\ndiff --git a/combine-diff.c b/combine-diff.c\nindex 9e163d5..acf39ec 100644\n--- a/combine-diff.c\n+++ b/combine-diff.c\n@@ -285,7 +285,7 @@ static struct lline *coalesce_lines(struct lline *base, int *lenbase,\n }\n \n static char *grab_blob(const struct object_id *oid, unsigned int mode,\n-\t\t       unsigned long *size, struct userdiff_driver *textconv,\n+\t\t       size_t *size, struct userdiff_driver *textconv,\n \t\t       const char *path)\n {\n \tchar *blob;\n@@ -401,7 +401,7 @@ static void combine_diff(const struct object_id *parent, unsigned int mode,\n \txdemitconf_t xecfg;\n \tmmfile_t parent_file;\n \tstruct combine_diff_state state;\n-\tunsigned long sz;\n+\tsize_t sz;\n \n \tif (result_deleted)\n \t\treturn; /* result deleted */\n@@ -971,7 +971,8 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,\n \t\t\t    struct rev_info *rev)\n {\n \tstruct diff_options *opt = &rev->diffopt;\n-\tunsigned long result_size, cnt, lno;\n+\tsize_t result_size;\n+\tunsigned long cnt, lno;\n \tint result_deleted = 0;\n \tchar *result, *cp;\n \tstruct sline *sline; /* survived lines */\n@@ -1087,7 +1088,7 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent,\n \t\tis_binary = buffer_is_binary(result, result_size);\n \t\tfor (i = 0; !is_binary && i < num_parent; i++) {\n \t\t\tchar *buf;\n-\t\t\tunsigned long size;\n+\t\t\tsize_t size;\n \t\t\tbuf = grab_blob(&elem->parent[i].oid,\n \t\t\t\t\telem->parent[i].mode,\n \t\t\t\t\t&size, NULL, NULL);\ndiff --git a/commit.c b/commit.c\nindex 8b28415..22e66b8 100644\n--- a/commit.c\n+++ b/commit.c\n@@ -261,7 +261,7 @@ const void *get_commit_buffer(const struct commit *commit, unsigned long *sizep)\n \tconst void *ret = get_cached_commit_buffer(commit, sizep);\n \tif (!ret) {\n \t\tenum object_type type;\n-\t\tunsigned long size;\n+\t\tsize_t size;\n \t\tret = read_sha1_file(commit->object.oid.hash, &type, &size);\n \t\tif (!ret)\n \t\t\tdie(\"cannot read commit object %s\",\n@@ -372,7 +372,7 @@ int parse_commit_gently(struct commit *item, int quiet_on_missing)\n {\n \tenum object_type type;\n \tvoid *buffer;\n-\tunsigned long size;\n+\tsize_t size;\n \tint ret;\n \n \tif (!item)\n@@ -1203,7 +1203,7 @@ static void handle_signed_tag(struct commit *parent, struct commit_extra_header\n \tstruct merge_remote_desc *desc;\n \tstruct commit_extra_header *mergetag;\n \tchar *buf;\n-\tunsigned long size, len;\n+\tsize_t size, len;\n \tenum object_type type;\n \n \tdesc = merge_remote_util(parent);\ndiff --git a/config.c b/config.c\nindex 44bee26..831cf8b 100644\n--- a/config.c\n+++ b/config.c\n@@ -1471,7 +1471,7 @@ int git_config_from_blob_oid(config_fn_t fn,\n {\n \tenum object_type type;\n \tchar *buf;\n-\tunsigned long size;\n+\tsize_t size;\n \tint ret;\n \n \tbuf = read_sha1_file(oid->hash, &type, &size);\ndiff --git a/delta.h b/delta.h\nindex 9b67531..2df0f55 100644\n--- a/delta.h\n+++ b/delta.h\n@@ -14,7 +14,7 @@ struct delta_index;\n  * using free_delta_index().\n  */\n extern struct delta_index *\n-create_delta_index(const void *buf, unsigned long bufsize);\n+create_delta_index(const void *buf, size_t bufsize);\n \n /*\n  * free_delta_index: free the index created by create_delta_index()\n@@ -28,7 +28,7 @@ extern void free_delta_index(struct delta_index *index);\n  *\n  * Given pointer must be what create_delta_index() returned, or NULL.\n  */\n-extern unsigned long sizeof_delta_index(struct delta_index *index);\n+extern size_t sizeof_delta_index(struct delta_index *index);\n \n /*\n  * create_delta: create a delta from given index for the given buffer\n@@ -42,8 +42,8 @@ extern unsigned long sizeof_delta_index(struct delta_index *index);\n  */\n extern void *\n create_delta(const struct delta_index *index,\n-\t     const void *buf, unsigned long bufsize,\n-\t     unsigned long *delta_size, unsigned long max_delta_size);\n+\t     const void *buf, size_t bufsize,\n+\t     size_t *delta_size, size_t max_delta_size);\n \n /*\n  * diff_delta: create a delta from source buffer to target buffer\n@@ -54,9 +54,9 @@ create_delta(const struct delta_index *index,\n  * updated with its size.  The returned buffer must be freed by the caller.\n  */\n static inline void *\n-diff_delta(const void *src_buf, unsigned long src_bufsize,\n-\t   const void *trg_buf, unsigned long trg_bufsize,\n-\t   unsigned long *delta_size, unsigned long max_delta_size)\n+diff_delta(const void *src_buf, size_t src_bufsize,\n+\t   const void *trg_buf, size_t trg_bufsize,\n+\t   size_t *delta_size, size_t max_delta_size)\n {\n \tstruct delta_index *index = create_delta_index(src_buf, src_bufsize);\n \tif (index) {\n@@ -75,9 +75,9 @@ diff_delta(const void *src_buf, unsigned long src_bufsize,\n  * *trg_bufsize is updated with its size.  On failure a NULL pointer is\n  * returned.  The returned buffer must be freed by the caller.\n  */\n-extern void *patch_delta(const void *src_buf, unsigned long src_size,\n-\t\t\t const void *delta_buf, unsigned long delta_size,\n-\t\t\t unsigned long *dst_size);\n+extern void *patch_delta(const void *src_buf, size_t src_size,\n+\t\t\t const void *delta_buf, size_t delta_size,\n+\t\t\t size_t *dst_size);\n \n /* the smallest possible delta size is 4 bytes */\n #define DELTA_SIZE_MIN\t4\n@@ -86,11 +86,11 @@ extern void *patch_delta(const void *src_buf, unsigned long src_size,\n  * This must be called twice on the delta data buffer, first to get the\n  * expected source buffer size, and again to get the target buffer size.\n  */\n-static inline unsigned long get_delta_hdr_size(const unsigned char **datap,\n-\t\t\t\t\t       const unsigned char *top)\n+static inline size_t get_delta_hdr_size(const unsigned char **datap,\n+\t\t\t\t\tconst unsigned char *top)\n {\n \tconst unsigned char *data = *datap;\n-\tunsigned long cmd, size = 0;\n+\tsize_t cmd, size = 0;\n \tint i = 0;\n \tdo {\n \t\tcmd = *data++;\ndiff --git a/diff-delta.c b/diff-delta.c\nindex cd238c8..3d5e1ef 100644\n--- a/diff-delta.c\n+++ b/diff-delta.c\n@@ -123,14 +123,14 @@ struct unpacked_index_entry {\n };\n \n struct delta_index {\n-\tunsigned long memsize;\n+\tsize_t memsize;\n \tconst void *src_buf;\n-\tunsigned long src_size;\n+\tsize_t src_size;\n \tunsigned int hash_mask;\n \tstruct index_entry *hash[FLEX_ARRAY];\n };\n \n-struct delta_index * create_delta_index(const void *buf, unsigned long bufsize)\n+struct delta_index * create_delta_index(const void *buf, size_t bufsize)\n {\n \tunsigned int i, hsize, hmask, entries, prev_val, *hash_count;\n \tconst unsigned char *data, *buffer = buf;\n@@ -138,7 +138,7 @@ struct delta_index * create_delta_index(const void *buf, unsigned long bufsize)\n \tstruct unpacked_index_entry *entry, **hash;\n \tstruct index_entry *packed_entry, **packed_hash;\n \tvoid *mem;\n-\tunsigned long memsize;\n+\tsize_t memsize;\n \n \tif (!buf || !bufsize)\n \t\treturn NULL;\n@@ -300,7 +300,7 @@ void free_delta_index(struct delta_index *index)\n \tfree(index);\n }\n \n-unsigned long sizeof_delta_index(struct delta_index *index)\n+size_t sizeof_delta_index(struct delta_index *index)\n {\n \tif (index)\n \t\treturn index->memsize;\n@@ -316,8 +316,8 @@ unsigned long sizeof_delta_index(struct delta_index *index)\n \n void *\n create_delta(const struct delta_index *index,\n-\t     const void *trg_buf, unsigned long trg_size,\n-\t     unsigned long *delta_size, unsigned long max_size)\n+\t     const void *trg_buf, size_t trg_size,\n+\t     size_t *delta_size, size_t max_size)\n {\n \tunsigned int i, val;\n \toff_t outpos, moff;\n@@ -407,7 +407,7 @@ create_delta(const struct delta_index *index,\n \t\t\t}\n \t\t\tmsize = 0;\n \t\t} else {\n-\t\t\tunsigned int left;\n+\t\t\tsize_t left;\n \t\t\tunsigned char *op;\n \n \t\t\tif (inscnt) {\ndiff --git a/diff.c b/diff.c\nindex f84346b..dd6ff0a 100644\n--- a/diff.c\n+++ b/diff.c\n@@ -2922,8 +2922,8 @@ static void checkdiff_consume(void *priv, char *line, unsigned long len)\n }\n \n static unsigned char *deflate_it(char *data,\n-\t\t\t\t unsigned long size,\n-\t\t\t\t unsigned long *result_size)\n+\t\t\t\t size_t size,\n+\t\t\t\t size_t *result_size)\n {\n \tint bound;\n \tunsigned char *deflated;\n@@ -2951,10 +2951,10 @@ static void emit_binary_diff_body(struct diff_options *o,\n \tvoid *delta;\n \tvoid *deflated;\n \tvoid *data;\n-\tunsigned long orig_size;\n-\tunsigned long delta_size;\n-\tunsigned long deflate_size;\n-\tunsigned long data_size;\n+\tsize_t orig_size;\n+\tsize_t delta_size;\n+\tsize_t deflate_size;\n+\tsize_t data_size;\n \n \t/* We could do deflated delta, or we could do just deflated two,\n \t * whichever is smaller.\n@@ -2974,7 +2974,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);\n@@ -2982,7 +2982,7 @@ static void emit_binary_diff_body(struct diff_options *o,\n \t\tdata = delta;\n \t\tdata_size = delta_size;\n \t} else {\n-\t\tchar *s = xstrfmt(\"%lu\", two->size);\n+\t\tchar *s = xstrfmt(\"%\" PRIuMAX, (uintmax_t)two->size);\n \t\temit_diff_symbol(o, DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL,\n \t\t\t\t s, strlen(s), 0);\n \t\tfree(s);\n@@ -6072,7 +6072,7 @@ int textconv_object(const char *path,\n \t\t    const struct object_id *oid,\n \t\t    int oid_valid,\n \t\t    char **buf,\n-\t\t    unsigned long *buf_size)\n+\t\t    size_t *buf_size)\n {\n \tstruct diff_filespec *df;\n \tstruct userdiff_driver *textconv;\ndiff --git a/diff.h b/diff.h\nindex 5755f46..3df6ccd 100644\n--- a/diff.h\n+++ b/diff.h\n@@ -417,7 +417,7 @@ extern struct userdiff_driver *get_textconv(struct diff_filespec *one);\n  * if the textconv driver exists.\n  * Return 1 if the conversion succeeds, 0 otherwise.\n  */\n-extern int textconv_object(const char *path, unsigned mode, const struct object_id *oid, int oid_valid, char **buf, unsigned long *buf_size);\n+extern int textconv_object(const char *path, unsigned mode, const struct object_id *oid, int oid_valid, char **buf, size_t *buf_size);\n \n extern int parse_rename_score(const char **cp_p);\n \ndiff --git a/diffcore.h b/diffcore.h\nindex a30da16..8ad1823 100644\n--- a/diffcore.h\n+++ b/diffcore.h\n@@ -29,7 +29,7 @@ struct diff_filespec {\n \tchar *path;\n \tvoid *data;\n \tvoid *cnt_data;\n-\tunsigned long size;\n+\tsize_t size;\n \tint count;               /* Reference count */\n \tint rename_used;         /* Count of rename users */\n \tunsigned short mode;\t /* file mode */\ndiff --git a/dir.c b/dir.c\nindex 1c55dc3..f161c26 100644\n--- a/dir.c\n+++ b/dir.c\n@@ -605,7 +605,7 @@ static void *read_skip_worktree_file_from_index(const struct index_state *istate\n \t\t\t\t\t\tstruct sha1_stat *sha1_stat)\n {\n \tint pos, len;\n-\tunsigned long sz;\n+\tsize_t sz;\n \tenum object_type type;\n \tvoid *data;\n \ndiff --git a/entry.c b/entry.c\nindex 65458f0..6db2d2e 100644\n--- a/entry.c\n+++ b/entry.c\n@@ -80,7 +80,7 @@ static int create_file(const char *path, unsigned int mode)\n \treturn open(path, O_WRONLY | O_CREAT | O_EXCL, mode);\n }\n \n-static void *read_blob_entry(const struct cache_entry *ce, unsigned long *size)\n+static void *read_blob_entry(const struct cache_entry *ce, size_t *size)\n {\n \tenum object_type type;\n \tvoid *new = read_sha1_file(ce->oid.hash, &type, size);\n@@ -243,7 +243,7 @@ static int write_entry(struct cache_entry *ce,\n \tint fd, ret, fstat_done = 0;\n \tchar *new;\n \tstruct strbuf buf = STRBUF_INIT;\n-\tunsigned long size;\n+\tsize_t size;\n \tsize_t wrote, newsize = 0;\n \tstruct stat st;\n \tconst struct submodule *sub;\ndiff --git a/fast-import.c b/fast-import.c\nindex a959161..334cabf 100644\n--- a/fast-import.c\n+++ b/fast-import.c\n@@ -1090,7 +1090,7 @@ static int store_object(\n \tstruct object_entry *e;\n \tunsigned char hdr[96];\n \tstruct object_id oid;\n-\tunsigned long hdrlen, deltalen;\n+\tsize_t hdrlen, deltalen;\n \tgit_SHA_CTX c;\n \tgit_zstream s;\n \n@@ -1345,7 +1345,7 @@ static void stream_blob(uintmax_t len, struct object_id *oidout, uintmax_t mark)\n  */\n static void *gfi_unpack_entry(\n \tstruct object_entry *oe,\n-\tunsigned long *sizep)\n+\tsize_t *sizep)\n {\n \tenum object_type type;\n \tstruct packed_git *p = all_packs[oe->pack_id];\n@@ -1391,7 +1391,7 @@ static void load_tree(struct tree_entry *root)\n \tstruct object_id *oid = &root->versions[1].oid;\n \tstruct object_entry *myoe;\n \tstruct tree_content *t;\n-\tunsigned long size;\n+\tsize_t size;\n \tchar *buf;\n \tconst char *c;\n \n@@ -2579,7 +2579,7 @@ static void note_change_n(const char *p, struct branch *b, unsigned char *old_fa\n \t\t\tdie(\"Mark :%\" PRIuMAX \" not a commit\", commit_mark);\n \t\toidcpy(&commit_oid, &commit_oe->idx.oid);\n \t} else if (!get_oid(p, &commit_oid)) {\n-\t\tunsigned long size;\n+\t\tsize_t size;\n \t\tchar *buf = read_object_with_reference(commit_oid.hash,\n \t\t\tcommit_type, &size, commit_oid.hash);\n \t\tif (!buf || size < 46)\n@@ -2647,7 +2647,7 @@ static void parse_from_existing(struct branch *b)\n \t\toidclr(&b->branch_tree.versions[0].oid);\n \t\toidclr(&b->branch_tree.versions[1].oid);\n \t} else {\n-\t\tunsigned long size;\n+\t\tsize_t size;\n \t\tchar *buf;\n \n \t\tbuf = read_object_with_reference(b->oid.hash,\n@@ -2685,7 +2685,7 @@ static int parse_from(struct branch *b)\n \t\tif (oidcmp(&b->oid, &oe->idx.oid)) {\n \t\t\toidcpy(&b->oid, &oe->idx.oid);\n \t\t\tif (oe->pack_id != MAX_PACK_ID) {\n-\t\t\t\tunsigned long size;\n+\t\t\t\tsize_t size;\n \t\t\t\tchar *buf = gfi_unpack_entry(oe, &size);\n \t\t\t\tparse_from_commit(b, buf, size);\n \t\t\t\tfree(buf);\n@@ -2728,7 +2728,7 @@ static struct hash_list *parse_merge(unsigned int *count)\n \t\t\t\tdie(\"Mark :%\" PRIuMAX \" not a commit\", idnum);\n \t\t\toidcpy(&n->oid, &oe->idx.oid);\n \t\t} else if (!get_oid(from, &n->oid)) {\n-\t\t\tunsigned long size;\n+\t\t\tsize_t size;\n \t\t\tchar *buf = read_object_with_reference(n->oid.hash,\n \t\t\t\tcommit_type, &size, n->oid.hash);\n \t\t\tif (!buf || size < 46)\n@@ -2958,7 +2958,7 @@ static void cat_blob_write(const char *buf, unsigned long size)\n static void cat_blob(struct object_entry *oe, struct object_id *oid)\n {\n \tstruct strbuf line = STRBUF_INIT;\n-\tunsigned long size;\n+\tsize_t size;\n \tenum object_type type = 0;\n \tchar *buf;\n \n@@ -2986,8 +2986,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), typename(type));\n \tstrbuf_reset(&line);\n-\tstrbuf_addf(&line, \"%s %s %lu\\n\", oid_to_hex(oid),\n-\t\t\t\t\t\ttypename(type), size);\n+\tstrbuf_addf(&line, \"%s %s %\" PRIuMAX \"\\n\", oid_to_hex(oid),\n+\t\t    typename(type), (uintmax_t)size);\n \tcat_blob_write(line.buf, line.len);\n \tstrbuf_release(&line);\n \tcat_blob_write(buf, size);\n@@ -3042,7 +3042,7 @@ static void parse_cat_blob(const char *p)\n static struct object_entry *dereference(struct object_entry *oe,\n \t\t\t\t\tstruct object_id *oid)\n {\n-\tunsigned long size;\n+\tsize_t size;\n \tchar *buf = NULL;\n \tif (!oe) {\n \t\tenum object_type type = sha1_object_info(oid->hash, NULL);\n@@ -3538,7 +3538,7 @@ int cmd_main(int argc, const char **argv)\n \t\tfprintf(stderr, \"Total branches:  %10lu (%10lu loads     )\\n\", branch_count, branch_load_count);\n \t\tfprintf(stderr, \"      marks:     %10\" PRIuMAX \" (%10\" PRIuMAX \" unique    )\\n\", (((uintmax_t)1) << marks->shift) * 1024, marks_set_count);\n \t\tfprintf(stderr, \"      atoms:     %10u\\n\", atom_cnt);\n-\t\tfprintf(stderr, \"Memory total:    %10\" PRIuMAX \" KiB\\n\", (total_allocd + alloc_count*sizeof(struct object_entry))/1024);\n+\t\tfprintf(stderr, \"Memory total:    %10\" PRIuMAX \" KiB\\n\", (uintmax_t)(total_allocd + alloc_count*sizeof(struct object_entry))/1024);\n \t\tfprintf(stderr, \"       pools:    %10lu KiB\\n\", (unsigned long)(total_allocd/1024));\n \t\tfprintf(stderr, \"     objects:    %10\" PRIuMAX \" KiB\\n\", (alloc_count*sizeof(struct object_entry))/1024);\n \t\tfprintf(stderr, \"---------------------------------------------------------------------\\n\");\ndiff --git a/fsck.c b/fsck.c\nindex 2d2d2e9..feca3a8 100644\n--- a/fsck.c\n+++ b/fsck.c\n@@ -796,7 +796,7 @@ static int fsck_commit(struct commit *commit, const char *data,\n }\n \n static int fsck_tag_buffer(struct tag *tag, const char *data,\n-\tunsigned long size, struct fsck_options *options)\n+\tsize_t size, struct fsck_options *options)\n {\n \tunsigned char sha1[20];\n \tint ret = 0;\ndiff --git a/grep.h b/grep.h\nindex 52aecfa..f810a93 100644\n--- a/grep.h\n+++ b/grep.h\n@@ -197,7 +197,7 @@ struct grep_source {\n \tvoid *identifier;\n \n \tchar *buf;\n-\tunsigned long size;\n+\tsize_t size;\n \n \tchar *path; /* for attribute lookups */\n \tstruct userdiff_driver *driver;\ndiff --git a/http-push.c b/http-push.c\nindex c91f40a..4c06802 100644\n--- a/http-push.c\n+++ b/http-push.c\n@@ -355,13 +355,14 @@ static void start_put(struct transfer_request *request)\n \tenum object_type type;\n \tchar hdr[50];\n \tvoid *unpacked;\n-\tunsigned long len;\n+\tsize_t len;\n \tint hdrlen;\n \tssize_t size;\n \tgit_zstream stream;\n \n \tunpacked = read_sha1_file(request->obj->oid.hash, &type, &len);\n-\thdrlen = xsnprintf(hdr, sizeof(hdr), \"%s %lu\", typename(type), len) + 1;\n+\thdrlen = xsnprintf(hdr, sizeof(hdr), \"%s %\" PRIuMAX,\n+\t\t\t   typename(type), (uintmax_t)len) + 1;\n \n \t/* Set it up */\n \tgit_deflate_init(&stream, zlib_compression_level);\ndiff --git a/mailmap.c b/mailmap.c\nindex cb921b4..323bbef 100644\n--- a/mailmap.c\n+++ b/mailmap.c\n@@ -216,7 +216,7 @@ static int read_mailmap_blob(struct string_list *map,\n {\n \tstruct object_id oid;\n \tchar *buf;\n-\tunsigned long size;\n+\tsize_t size;\n \tenum object_type type;\n \n \tif (!name)\ndiff --git a/match-trees.c b/match-trees.c\nindex 396b733..d4b7c1e 100644\n--- a/match-trees.c\n+++ b/match-trees.c\n@@ -52,7 +52,7 @@ static void *fill_tree_desc_strict(struct tree_desc *desc,\n {\n \tvoid *buffer;\n \tenum object_type type;\n-\tunsigned long size;\n+\tsize_t size;\n \n \tbuffer = read_sha1_file(hash->hash, &type, &size);\n \tif (!buffer)\n@@ -169,7 +169,7 @@ static int splice_tree(const unsigned char *hash1,\n \tchar *subpath;\n \tint toplen;\n \tchar *buf;\n-\tunsigned long sz;\n+\tsize_t sz;\n \tstruct tree_desc desc;\n \tunsigned char *rewrite_here;\n \tconst unsigned char *rewrite_with;\ndiff --git a/merge-blobs.c b/merge-blobs.c\nindex 9b6eac2..f7e55c4 100644\n--- a/merge-blobs.c\n+++ b/merge-blobs.c\n@@ -8,7 +8,7 @@\n static int fill_mmfile_blob(mmfile_t *f, struct blob *obj)\n {\n \tvoid *buf;\n-\tunsigned long size;\n+\tsize_t size;\n \tenum object_type type;\n \n \tbuf = read_sha1_file(obj->object.oid.hash, &type, &size);\n@@ -28,7 +28,7 @@ static void free_mmfile(mmfile_t *f)\n \tfree(f->ptr);\n }\n \n-static void *three_way_filemerge(const char *path, mmfile_t *base, mmfile_t *our, mmfile_t *their, unsigned long *size)\n+static void *three_way_filemerge(const char *path, mmfile_t *base, mmfile_t *our, mmfile_t *their, size_t *size)\n {\n \tint merge_status;\n \tmmbuffer_t res;\n@@ -48,7 +48,7 @@ static void *three_way_filemerge(const char *path, mmfile_t *base, mmfile_t *our\n \treturn res.ptr;\n }\n \n-void *merge_blobs(const char *path, struct blob *base, struct blob *our, struct blob *their, unsigned long *size)\n+void *merge_blobs(const char *path, struct blob *base, struct blob *our, struct blob *their, size_t *size)\n {\n \tvoid *res = NULL;\n \tmmfile_t f1, f2, common;\ndiff --git a/merge-blobs.h b/merge-blobs.h\nindex 62b569e..e66eee7 100644\n--- a/merge-blobs.h\n+++ b/merge-blobs.h\n@@ -3,6 +3,6 @@\n \n #include \"blob.h\"\n \n-extern void *merge_blobs(const char *, struct blob *, struct blob *, struct blob *, unsigned long *);\n+extern void *merge_blobs(const char *, struct blob *, struct blob *, struct blob *, size_t *);\n \n #endif /* MERGE_BLOBS_H */\ndiff --git a/merge-recursive.c b/merge-recursive.c\nindex 1494ffd..e6b547c 100644\n--- a/merge-recursive.c\n+++ b/merge-recursive.c\n@@ -787,7 +787,7 @@ static int update_file_flags(struct merge_options *o,\n \tif (update_wd) {\n \t\tenum object_type type;\n \t\tvoid *buf;\n-\t\tunsigned long size;\n+\t\tsize_t size;\n \n \t\tif (S_ISGITLINK(mode)) {\n \t\t\t/*\n@@ -1602,7 +1602,7 @@ static int read_oid_strbuf(struct merge_options *o,\n {\n \tvoid *buf;\n \tenum object_type type;\n-\tunsigned long size;\n+\tsize_t size;\n \tbuf = read_sha1_file(oid->hash, &type, &size);\n \tif (!buf)\n \t\treturn err(o, _(\"cannot read object %s\"), oid_to_hex(oid));\ndiff --git a/notes-cache.c b/notes-cache.c\nindex 29b4ced..df86765 100644\n--- a/notes-cache.c\n+++ b/notes-cache.c\n@@ -72,7 +72,7 @@ char *notes_cache_get(struct notes_cache *c, struct object_id *key_oid,\n \tconst struct object_id *value_oid;\n \tenum object_type type;\n \tchar *value;\n-\tunsigned long size;\n+\tsize_t size;\n \n \tvalue_oid = get_note(&c->tree, key_oid);\n \tif (!value_oid)\ndiff --git a/notes-merge.c b/notes-merge.c\nindex c12b354..b1bf7ff 100644\n--- a/notes-merge.c\n+++ b/notes-merge.c\n@@ -323,7 +323,7 @@ static void write_note_to_worktree(const struct object_id *obj,\n \t\t\t\t   const struct object_id *note)\n {\n \tenum object_type type;\n-\tunsigned long size;\n+\tsize_t size;\n \tvoid *buf = read_sha1_file(note->hash, &type, &size);\n \n \tif (!buf)\ndiff --git a/notes.c b/notes.c\nindex 503754d..5b9bb96 100644\n--- a/notes.c\n+++ b/notes.c\n@@ -808,7 +808,7 @@ int combine_notes_concatenate(unsigned char *cur_sha1,\n \t\tconst unsigned char *new_sha1)\n {\n \tchar *cur_msg = NULL, *new_msg = NULL, *buf;\n-\tunsigned long cur_len, new_len, buf_len;\n+\tsize_t cur_len, new_len, buf_len;\n \tenum object_type cur_type, new_type;\n \tint ret;\n \n@@ -869,7 +869,7 @@ static int string_list_add_note_lines(struct string_list *list,\n \t\t\t\t      const unsigned char *sha1)\n {\n \tchar *data;\n-\tunsigned long len;\n+\tsize_t len;\n \tenum object_type t;\n \n \tif (is_null_sha1(sha1))\n@@ -1222,7 +1222,7 @@ static void format_note(struct notes_tree *t, const struct object_id *object_oid\n \tstatic const char utf8[] = \"utf-8\";\n \tconst struct object_id *oid;\n \tchar *msg, *msg_p;\n-\tunsigned long linelen, msglen;\n+\tsize_t linelen, msglen;\n \tenum object_type type;\n \n \tif (!t)\ndiff --git a/object.c b/object.c\nindex 321d7e9..dff9f52 100644\n--- a/object.c\n+++ b/object.c\n@@ -241,7 +241,7 @@ struct object *parse_object_or_die(const struct object_id *oid,\n \n struct object *parse_object(const struct object_id *oid)\n {\n-\tunsigned long size;\n+\tsize_t size;\n \tenum object_type type;\n \tint eaten;\n \tconst unsigned char *repl = lookup_replace_object(oid->hash);\ndiff --git a/pack-check.c b/pack-check.c\nindex e1fcb22..6f7714f 100644\n--- a/pack-check.c\n+++ b/pack-check.c\n@@ -104,7 +104,7 @@ static int verify_packfile(struct packed_git *p,\n \tfor (i = 0; i < nr_objects; i++) {\n \t\tvoid *data;\n \t\tenum object_type type;\n-\t\tunsigned long size;\n+\t\tsize_t size;\n \t\toff_t curpos;\n \t\tint data_valid;\n \ndiff --git a/pack-objects.h b/pack-objects.h\nindex 03f1191..3a2cf4e 100644\n--- a/pack-objects.h\n+++ b/pack-objects.h\n@@ -3,7 +3,7 @@\n \n struct object_entry {\n \tstruct pack_idx_entry idx;\n-\tunsigned long size;\t/* uncompressed size */\n+\tsize_t size;\t/* uncompressed size */\n \tstruct packed_git *in_pack;\t/* already in pack */\n \toff_t in_pack_offset;\n \tstruct object_entry *delta;\t/* delta base object */\n@@ -12,8 +12,8 @@ struct object_entry {\n \t\t\t\t\t     * uses the same base as me\n \t\t\t\t\t     */\n \tvoid *delta_data;\t/* cached delta (uncompressed) */\n-\tunsigned long delta_size;\t/* delta data size (uncompressed) */\n-\tunsigned long z_delta_size;\t/* delta data size (compressed) */\n+\tsize_t delta_size;\t/* delta data size (uncompressed) */\n+\tsize_t z_delta_size;\t/* delta data size (compressed) */\n \tenum object_type type;\n \tenum object_type in_pack_type;\t/* could be delta */\n \tuint32_t hash;\t\t\t/* name hint hash */\ndiff --git a/patch-delta.c b/patch-delta.c\nindex 56e0a5e..8b2fa20 100644\n--- a/patch-delta.c\n+++ b/patch-delta.c\n@@ -12,13 +12,13 @@\n #include \"git-compat-util.h\"\n #include \"delta.h\"\n \n-void *patch_delta(const void *src_buf, unsigned long src_size,\n-\t\t  const void *delta_buf, unsigned long delta_size,\n-\t\t  unsigned long *dst_size)\n+void *patch_delta(const void *src_buf, size_t src_size,\n+\t\t  const void *delta_buf, size_t delta_size,\n+\t\t  size_t *dst_size)\n {\n \tconst unsigned char *data, *top;\n \tunsigned char *dst_buf, *out, cmd;\n-\tunsigned long size;\n+\tsize_t size;\n \n \tif (delta_size < DELTA_SIZE_MIN)\n \t\treturn NULL;\n@@ -39,7 +39,8 @@ void *patch_delta(const void *src_buf, unsigned long src_size,\n \twhile (data < top) {\n \t\tcmd = *data++;\n \t\tif (cmd & 0x80) {\n-\t\t\tunsigned long cp_off = 0, cp_size = 0;\n+\t\t\toff_t cp_off = 0;\n+\t\t\tsize_t cp_size = 0;\n \t\t\tif (cmd & 0x01) cp_off = *data++;\n \t\t\tif (cmd & 0x02) cp_off |= (*data++ << 8);\n \t\t\tif (cmd & 0x04) cp_off |= (*data++ << 16);\ndiff --git a/read-cache.c b/read-cache.c\nindex acfb028..854a5d6 100644\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -172,7 +172,7 @@ static int ce_compare_link(const struct cache_entry *ce, size_t expected_size)\n {\n \tint match = -1;\n \tvoid *buffer;\n-\tunsigned long size;\n+\tsize_t size;\n \tenum object_type type;\n \tstruct strbuf sb = STRBUF_INIT;\n \n@@ -2583,7 +2583,7 @@ void *read_blob_data_from_index(const struct index_state *istate,\n \t\t\t\tconst char *path, unsigned long *size)\n {\n \tint pos, len;\n-\tunsigned long sz;\n+\tsize_t sz;\n \tenum object_type type;\n \tvoid *data;\n \ndiff --git a/ref-filter.c b/ref-filter.c\nindex bc591f4..5c903a5 100644\n--- a/ref-filter.c\n+++ b/ref-filter.c\n@@ -692,7 +692,7 @@ int verify_ref_format(struct ref_format *format)\n  * by the \"struct object\" representation, set *eaten as well---it is a\n  * signal from parse_object_buffer to us not to free the buffer.\n  */\n-static void *get_obj(const struct object_id *oid, struct object **obj, unsigned long *sz, int *eaten)\n+static void *get_obj(const struct object_id *oid, struct object **obj, size_t *sz, int *eaten)\n {\n \tenum object_type type;\n \tvoid *buf = read_sha1_file(oid->hash, &type, sz);\n@@ -1311,7 +1311,7 @@ static void populate_value(struct ref_array_item *ref)\n \tvoid *buf;\n \tstruct object *obj;\n \tint eaten, i;\n-\tunsigned long size;\n+\tsize_t size;\n \tconst struct object_id *tagged;\n \n \tref->value = xcalloc(used_atom_cnt, sizeof(struct atom_value));\ndiff --git a/remote-testsvn.c b/remote-testsvn.c\nindex 0ff4a31..08748ca 100644\n--- a/remote-testsvn.c\n+++ b/remote-testsvn.c\n@@ -55,7 +55,7 @@ static char *read_ref_note(const struct object_id *oid)\n {\n \tconst struct object_id *note_oid;\n \tchar *msg = NULL;\n-\tunsigned long msglen;\n+\tsize_t msglen;\n \tenum object_type type;\n \n \tinit_notes(NULL, notes_ref, NULL, 0);\n@@ -104,7 +104,7 @@ static int note2mark_cb(const struct object_id *object_oid,\n {\n \tFILE *file = (FILE *)cb_data;\n \tchar *msg;\n-\tunsigned long msglen;\n+\tsize_t msglen;\n \tenum object_type type;\n \tstruct rev_note note;\n \ndiff --git a/rerere.c b/rerere.c\nindex 70634d4..41852f0 100644\n--- a/rerere.c\n+++ b/rerere.c\n@@ -974,7 +974,7 @@ static int handle_cache(const char *path, unsigned char *sha1, const char *outpu\n \n \twhile (pos < active_nr) {\n \t\tenum object_type type;\n-\t\tunsigned long size;\n+\t\tsize_t size;\n \n \t\tce = active_cache[pos++];\n \t\tif (ce_namelen(ce) != len || memcmp(ce->name, path, len))\ndiff --git a/sha1_file.c b/sha1_file.c\nindex b60ae15..97b39b0 100644\n--- a/sha1_file.c\n+++ b/sha1_file.c\n@@ -1631,7 +1631,7 @@ static const struct packed_git *has_packed_and_bad(const unsigned char *sha1)\n  * the streaming interface and rehash it to do the same.\n  */\n int check_sha1_signature(const unsigned char *sha1, void *map,\n-\t\t\t unsigned long size, const char *type)\n+\t\t\t size_t size, const char *type)\n {\n \tunsigned char real_sha1[20];\n \tenum object_type obj_type;\n@@ -1650,7 +1650,8 @@ int check_sha1_signature(const unsigned char *sha1, void *map,\n \t\treturn -1;\n \n \t/* Generate the header */\n-\thdrlen = xsnprintf(hdr, sizeof(hdr), \"%s %lu\", typename(obj_type), size) + 1;\n+\thdrlen = xsnprintf(hdr, sizeof(hdr), \"%s %\" PRIuMAX,\n+\t\t\t   typename(obj_type), (uintmax_t)size) + 1;\n \n \t/* Sha1.. */\n \tgit_SHA1_Init(&c);\n@@ -1795,11 +1796,11 @@ void *map_sha1_file(const unsigned char *sha1, unsigned long *size)\n }\n \n unsigned long unpack_object_header_buffer(const unsigned char *buf,\n-\t\tunsigned long len, enum object_type *type, unsigned long *sizep)\n+\t\tunsigned long len, enum object_type *type, size_t *sizep)\n {\n \tunsigned shift;\n-\tunsigned long size, c;\n-\tunsigned long used = 0;\n+\tsize_t size, c;\n+\tsize_t used = 0;\n \n \tc = buf[used++];\n \t*type = (c >> 4) & 7;\n@@ -1997,7 +1998,7 @@ static int parse_sha1_header_extended(const char *hdr, struct object_info *oi,\n \treturn *hdr ? -1 : type;\n }\n \n-int parse_sha1_header(const char *hdr, unsigned long *sizep)\n+int parse_sha1_header(const char *hdr, size_t *sizep)\n {\n \tstruct object_info oi = OBJECT_INFO_INIT;\n \n@@ -2118,7 +2119,7 @@ static const unsigned char *get_delta_base_sha1(struct packed_git *p,\n int unpack_object_header(struct packed_git *p,\n \t\t\t struct pack_window **w_curs,\n \t\t\t off_t *curpos,\n-\t\t\t unsigned long *sizep)\n+\t\t\t size_t *sizep)\n {\n \tunsigned char *base;\n \tunsigned long left;\n@@ -2171,7 +2172,7 @@ static enum object_type packed_to_object_type(struct packed_git *p,\n \n \twhile (type == OBJ_OFS_DELTA || type == OBJ_REF_DELTA) {\n \t\toff_t base_offset;\n-\t\tunsigned long size;\n+\t\tsize_t size;\n \t\t/* Push the object we're going to leave behind */\n \t\tif (poi_stack_nr >= poi_stack_alloc && poi_stack == small_poi_stack) {\n \t\t\tpoi_stack_alloc = alloc_nr(poi_stack_nr);\n@@ -2306,7 +2307,7 @@ static void detach_delta_base_cache_entry(struct delta_base_cache_entry *ent)\n }\n \n static void *cache_or_unpack_entry(struct packed_git *p, off_t base_offset,\n-\tunsigned long *base_size, enum object_type *type)\n+\tsize_t *base_size, enum object_type *type)\n {\n \tstruct delta_base_cache_entry *ent;\n \n@@ -2370,7 +2371,7 @@ int packed_object_info(struct packed_git *p, off_t obj_offset,\n \t\t       struct object_info *oi)\n {\n \tstruct pack_window *w_curs = NULL;\n-\tunsigned long size;\n+\tsize_t size;\n \toff_t curpos = obj_offset;\n \tenum object_type type;\n \n@@ -2484,7 +2485,7 @@ static void *unpack_compressed_entry(struct packed_git *p,\n }\n \n static void *read_object(const unsigned char *sha1, enum object_type *type,\n-\t\t\t unsigned long *size);\n+\t\t\t size_t *size);\n \n static void write_pack_access_log(struct packed_git *p, off_t obj_offset)\n {\n@@ -2503,12 +2504,12 @@ struct unpack_entry_stack_ent {\n };\n \n void *unpack_entry(struct packed_git *p, off_t obj_offset,\n-\t\t   enum object_type *final_type, unsigned long *final_size)\n+\t\t   enum object_type *final_type, size_t *final_size)\n {\n \tstruct pack_window *w_curs = NULL;\n \toff_t curpos = obj_offset;\n \tvoid *data = NULL;\n-\tunsigned long size;\n+\tsize_t size;\n \tenum object_type type;\n \tstruct unpack_entry_stack_ent small_delta_stack[UNPACK_ENTRY_STACK_PREALLOC];\n \tstruct unpack_entry_stack_ent *delta_stack = small_delta_stack;\n@@ -2608,7 +2609,7 @@ void *unpack_entry(struct packed_git *p, off_t obj_offset,\n \t\tvoid *delta_data;\n \t\tvoid *base = data;\n \t\tvoid *external_base = NULL;\n-\t\tunsigned long delta_size, base_size = size;\n+\t\tsize_t delta_size, base_size = size;\n \t\tint i;\n \n \t\tdata = NULL;\n@@ -2913,7 +2914,7 @@ static int sha1_loose_object_info(const unsigned char *sha1,\n \tgit_zstream stream;\n \tchar hdr[32];\n \tstruct strbuf hdrbuf = STRBUF_INIT;\n-\tunsigned long size_scratch;\n+\tsize_t size_scratch;\n \n \tif (oi->delta_base_sha1)\n \t\thashclr(oi->delta_base_sha1);\n@@ -3050,7 +3051,7 @@ int sha1_object_info_extended(const unsigned char *sha1, struct object_info *oi,\n }\n \n /* returns enum object_type or negative */\n-int sha1_object_info(const unsigned char *sha1, unsigned long *sizep)\n+int sha1_object_info(const unsigned char *sha1, size_t *sizep)\n {\n \tenum object_type type;\n \tstruct object_info oi = OBJECT_INFO_INIT;\n@@ -3064,7 +3065,7 @@ int sha1_object_info(const unsigned char *sha1, unsigned long *sizep)\n }\n \n static void *read_packed_sha1(const unsigned char *sha1,\n-\t\t\t      enum object_type *type, unsigned long *size)\n+\t\t\t      enum object_type *type, size_t *size)\n {\n \tstruct pack_entry e;\n \tvoid *data;\n@@ -3106,7 +3107,7 @@ int pretend_sha1_file(void *buf, unsigned long len, enum object_type type,\n }\n \n static void *read_object(const unsigned char *sha1, enum object_type *type,\n-\t\t\t unsigned long *size)\n+\t\t\t size_t *size)\n {\n \tstruct object_info oi = OBJECT_INFO_INIT;\n \tvoid *content;\n@@ -3126,7 +3127,7 @@ static void *read_object(const unsigned char *sha1, enum object_type *type,\n  */\n void *read_sha1_file_extended(const unsigned char *sha1,\n \t\t\t      enum object_type *type,\n-\t\t\t      unsigned long *size,\n+\t\t\t      size_t *size,\n \t\t\t      int lookup_replace)\n {\n \tvoid *data;\n@@ -3162,12 +3163,12 @@ void *read_sha1_file_extended(const unsigned char *sha1,\n \n void *read_object_with_reference(const unsigned char *sha1,\n \t\t\t\t const char *required_type_name,\n-\t\t\t\t unsigned long *size,\n+\t\t\t\t size_t *size,\n \t\t\t\t unsigned char *actual_sha1_return)\n {\n \tenum object_type type, required_type;\n \tvoid *buffer;\n-\tunsigned long isize;\n+\tsize_t isize;\n \tunsigned char actual_sha1[20];\n \n \trequired_type = type_from_string(required_type_name);\n@@ -3461,7 +3462,7 @@ int hash_sha1_file_literally(const void *buf, unsigned long len, const char *typ\n int force_object_loose(const unsigned char *sha1, time_t mtime)\n {\n \tvoid *buf;\n-\tunsigned long len;\n+\tsize_t len;\n \tenum object_type type;\n \tchar hdr[32];\n \tint hdrlen;\n@@ -3472,7 +3473,8 @@ int force_object_loose(const unsigned char *sha1, time_t mtime)\n \tbuf = read_packed_sha1(sha1, &type, &len);\n \tif (!buf)\n \t\treturn error(\"cannot read sha1_file for %s\", sha1_to_hex(sha1));\n-\thdrlen = xsnprintf(hdr, sizeof(hdr), \"%s %lu\", typename(type), len) + 1;\n+\thdrlen = xsnprintf(hdr, sizeof(hdr), \"%s %\" PRIuMAX,\n+\t\t\t   typename(type), (uintmax_t)len) + 1;\n \tret = write_loose_object(sha1, hdr, hdrlen, buf, len, mtime);\n \tfree(buf);\n \n@@ -3985,7 +3987,7 @@ static int check_stream_sha1(git_zstream *stream,\n int read_loose_object(const char *path,\n \t\t      const unsigned char *expected_sha1,\n \t\t      enum object_type *type,\n-\t\t      unsigned long *size,\n+\t\t      size_t *size,\n \t\t      void **contents)\n {\n \tint ret = -1;\ndiff --git a/streaming.c b/streaming.c\nindex 9afa66b..04a8b99 100644\n--- a/streaming.c\n+++ b/streaming.c\n@@ -65,7 +65,7 @@ struct filtered_istream {\n \n struct git_istream {\n \tconst struct stream_vtbl *vtbl;\n-\tunsigned long size; /* inflated size of full object */\n+\tsize_t size; /* inflated size of full object */\n \tgit_zstream z;\n \tenum { z_unused, z_used, z_done, z_error } z_state;\n \n@@ -108,7 +108,7 @@ static enum input_source istream_source(const unsigned char *sha1,\n \t\t\t\t\tenum object_type *type,\n \t\t\t\t\tstruct object_info *oi)\n {\n-\tunsigned long size;\n+\tsize_t size;\n \tint status;\n \n \toi->typep = type;\n@@ -131,7 +131,7 @@ static enum input_source istream_source(const unsigned char *sha1,\n \n struct git_istream *open_istream(const unsigned char *sha1,\n \t\t\t\t enum object_type *type,\n-\t\t\t\t unsigned long *size,\n+\t\t\t\t size_t *size,\n \t\t\t\t struct stream_filter *filter)\n {\n \tstruct git_istream *st;\n@@ -502,7 +502,7 @@ int stream_blob_to_fd(int fd, const struct object_id *oid, struct stream_filter\n {\n \tstruct git_istream *st;\n \tenum object_type type;\n-\tunsigned long sz;\n+\tsize_t sz;\n \tssize_t kept = 0;\n \tint result = -1;\n \ndiff --git a/streaming.h b/streaming.h\nindex 73c1d15..49f42c3 100644\n--- a/streaming.h\n+++ b/streaming.h\n@@ -8,7 +8,7 @@\n /* opaque */\n struct git_istream;\n \n-extern struct git_istream *open_istream(const unsigned char *, enum object_type *, unsigned long *, struct stream_filter *);\n+extern struct git_istream *open_istream(const unsigned char *, enum object_type *, size_t *, struct stream_filter *);\n extern int close_istream(struct git_istream *);\n extern ssize_t read_istream(struct git_istream *, void *, size_t);\n \ndiff --git a/submodule-config.c b/submodule-config.c\nindex 2b83c23..fa385e6 100644\n--- a/submodule-config.c\n+++ b/submodule-config.c\n@@ -482,7 +482,7 @@ static const struct submodule *config_from(struct submodule_cache *cache,\n \t\tenum lookup_type lookup_type)\n {\n \tstruct strbuf rev = STRBUF_INIT;\n-\tunsigned long config_size;\n+\tsize_t config_size;\n \tchar *config = NULL;\n \tstruct object_id oid;\n \tenum object_type type;\ndiff --git a/t/helper/test-delta.c b/t/helper/test-delta.c\nindex 59937dc..6524fbd 100644\n--- a/t/helper/test-delta.c\n+++ b/t/helper/test-delta.c\n@@ -20,7 +20,7 @@ int cmd_main(int argc, const char **argv)\n \tint fd;\n \tstruct stat st;\n \tvoid *from_buf, *data_buf, *out_buf;\n-\tunsigned long from_size, data_size, out_size;\n+\tsize_t from_size, data_size, out_size;\n \n \tif (argc != 5 || (strcmp(argv[1], \"-d\") && strcmp(argv[1], \"-p\"))) {\n \t\tfprintf(stderr, \"usage: %s\\n\", usage_str);\ndiff --git a/tag.c b/tag.c\nindex 7e10acf..b6bb5ea 100644\n--- a/tag.c\n+++ b/tag.c\n@@ -38,7 +38,7 @@ int gpg_verify_tag(const struct object_id *oid, const char *name_to_report,\n {\n \tenum object_type type;\n \tchar *buf;\n-\tunsigned long size;\n+\tsize_t size;\n \tint ret;\n \n \ttype = sha1_object_info(oid->hash, NULL);\n@@ -177,7 +177,7 @@ int parse_tag(struct tag *item)\n {\n \tenum object_type type;\n \tvoid *data;\n-\tunsigned long size;\n+\tsize_t size;\n \tint ret;\n \n \tif (item->object.parsed)\ndiff --git a/tree-walk.c b/tree-walk.c\nindex 6a42e40..7c9f9e3 100644\n--- a/tree-walk.c\n+++ b/tree-walk.c\n@@ -80,7 +80,7 @@ int init_tree_desc_gently(struct tree_desc *desc, const void *buffer, unsigned l\n \n void *fill_tree_descriptor(struct tree_desc *desc, const unsigned char *sha1)\n {\n-\tunsigned long size = 0;\n+\tsize_t size = 0;\n \tvoid *buf = NULL;\n \n \tif (sha1) {\n@@ -530,7 +530,7 @@ int get_tree_entry(const unsigned char *tree_sha1, const char *name, unsigned ch\n {\n \tint retval;\n \tvoid *tree;\n-\tunsigned long size;\n+\tsize_t size;\n \tunsigned char root[20];\n \n \ttree = read_object_with_reference(tree_sha1, tree_type, &size, root);\n@@ -600,7 +600,7 @@ enum follow_symlinks_result get_tree_entry_follow_symlinks(unsigned char *tree_s\n \t\tif (!t.buffer) {\n \t\t\tvoid *tree;\n \t\t\tunsigned char root[20];\n-\t\t\tunsigned long size;\n+\t\t\tsize_t size;\n \t\t\ttree = read_object_with_reference(current_tree_sha1,\n \t\t\t\t\t\t\t  tree_type, &size,\n \t\t\t\t\t\t\t  root);\n@@ -696,7 +696,7 @@ enum follow_symlinks_result get_tree_entry_follow_symlinks(unsigned char *tree_s\n \t\t\tgoto done;\n \t\t} else if (S_ISLNK(*mode)) {\n \t\t\t/* Follow a symlink */\n-\t\t\tunsigned long link_len;\n+\t\t\tsize_t link_len;\n \t\t\tsize_t len;\n \t\t\tchar *contents, *contents_start;\n \t\t\tstruct dir_state *parent;\ndiff --git a/tree.c b/tree.c\nindex b224115..5e42f83 100644\n--- a/tree.c\n+++ b/tree.c\n@@ -215,7 +215,7 @@ int parse_tree_gently(struct tree *item, int quiet_on_missing)\n {\n \t enum object_type type;\n \t void *buffer;\n-\t unsigned long size;\n+\t size_t size;\n \n \tif (item->object.parsed)\n \t\treturn 0;\ndiff --git a/xdiff-interface.c b/xdiff-interface.c\nindex 018e033..d82cd4a 100644\n--- a/xdiff-interface.c\n+++ b/xdiff-interface.c\n@@ -181,7 +181,7 @@ int read_mmfile(mmfile_t *ptr, const char *filename)\n \n void read_mmblob(mmfile_t *ptr, const struct object_id *oid)\n {\n-\tunsigned long size;\n+\tsize_t size;\n \tenum object_type type;\n \n \tif (!oidcmp(oid, &null_oid)) {\n-- \n2.1.4\n\n"},{"id":"326029","messageId":"alpine.DEB.2.21.1.1708101646030.11175@virtualbox","threadId":"46552","inReplyTo":"1502348462-4992-2-git-send-email-martin@mail.zuhause","subject":"Re: [PATCH V2 2/2] Convert size datatype to size_t","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2017-08-10T14:46:15Z","receivedAt":"2017-08-10T14:46:33Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Martin,\n\nOn Thu, 10 Aug 2017, Martin Koegler wrote:\n\n> From: Martin Koegler <martin.koegler@chello.at>\n> \n> It changes the signature of the core object access function\n> including any other functions to assure a clean compile if\n> sizeof(size_t) != sizeof(unsigned long).\n> \n> Signed-off-by: Martin Koegler <martin.koegler@chello.at>\n> ---\n> For next. As this touches core functions, it will likely produce\n> conflicts with other changes. Please provide the commit you want\n> to rebase the patch on and I'll produce a V3.\n> \n> Includes changes from Johannes Schindelin.\n\nThank you so much!\nDscho\n"},{"id":"326087","messageId":"xmqqmv772nmc.fsf@gitster.mtv.corp.google.com","threadId":"46552","inReplyTo":"1502348462-4992-1-git-send-email-martin@mail.zuhause","subject":"Re: [PATCH V2 1/2] Fix delta integer overflows","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2017-08-10T20:07:07Z","receivedAt":"2017-08-10T20:07:15Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Martin Koegler <martin.koegler@chello.at> writes:\n\n> From: Martin Koegler <martin.koegler@chello.at>\n\nJust a nitpick on the patch title.  As \"git shortlog --no-merges\"\noutput would tell you, we try to prefix the title with a short name\nof the area of codebase we are touching, followed by a colon and a\nspace and then remainder without extra capitalization.  Perhaps\n\n    Subject: delta: fix enconding size larger than an \"uint\" can hold\n\n> The current delta code produces incorrect pack objects for files > 4GB.\n>\n> Signed-off-by: Martin Koegler <martin.koegler@chello.at>\n\nI am a bit torn on this change.\n\nThe original is indeed bad in that the code does not guarantee that\nan intermediate variable like 'l' is not large enough to hold the\ntrue size we know in index->src_size, and in that sense this change\nis an improvement.\n\nGiven that this is not merely a local storage format but it also is\nan interchange format, we would probably want to make sure that the\nreceiving end (e.g. get_delta_hdr_size() that is used at the\nbeginning of patch_delta()) on a platform whose size_t is smaller\nthan that of a platform that produced the delta stream with this\ncode behaves \"sensibly\".\n\nIf we replaced ulong we use in create/patch delta codepaths with\nuint32_t, that would be safer, just because the encoder would not be\nable to emit varint that is larger than the receivers to handle.\nBut that defeats the whole point of using varint() to encode the\nsizes in the first place.  It was partly done for space saving, but\nmore for allowing larger sizes and larger ulong in the future\nwithout having to change the file format.\n\nPerhaps we should teach the receiving end to notice that the varint\ndata it reads encodes a size that is too large for it to grok and\ndie.  With that, we can safely move forward with whatever size_t\neach platform uses.\n\nThanks.\n\n> ---\n> For next.\n>\n>  diff-delta.c | 24 +++++++++++++-----------\n>  1 file changed, 13 insertions(+), 11 deletions(-)\n>\n> diff --git a/diff-delta.c b/diff-delta.c\n> index 3797ce6..cd238c8 100644\n> --- a/diff-delta.c\n> +++ b/diff-delta.c\n> @@ -319,7 +319,9 @@ create_delta(const struct delta_index *index,\n>  \t     const void *trg_buf, unsigned long trg_size,\n>  \t     unsigned long *delta_size, unsigned long max_size)\n>  {\n> -\tunsigned int i, outpos, outsize, moff, msize, val;\n> +\tunsigned int i, val;\n> +\toff_t outpos, moff;\n> +\tsize_t l, outsize, msize;\n>  \tint inscnt;\n>  \tconst unsigned char *ref_data, *ref_top, *data, *top;\n>  \tunsigned char *out;\n> @@ -336,20 +338,20 @@ create_delta(const struct delta_index *index,\n>  \t\treturn NULL;\n>  \n>  \t/* store reference buffer size */\n> -\ti = index->src_size;\n> -\twhile (i >= 0x80) {\n> -\t\tout[outpos++] = i | 0x80;\n> -\t\ti >>= 7;\n> +\tl = index->src_size;\n> +\twhile (l >= 0x80) {\n> +\t\tout[outpos++] = l | 0x80;\n> +\t\tl >>= 7;\n>  \t}\n> -\tout[outpos++] = i;\n> +\tout[outpos++] = l;\n>  \n>  \t/* store target buffer size */\n> -\ti = trg_size;\n> -\twhile (i >= 0x80) {\n> -\t\tout[outpos++] = i | 0x80;\n> -\t\ti >>= 7;\n> +\tl = trg_size;\n> +\twhile (l >= 0x80) {\n> +\t\tout[outpos++] = l | 0x80;\n> +\t\tl >>= 7;\n>  \t}\n> -\tout[outpos++] = i;\n> +\tout[outpos++] = l;\n>  \n>  \tref_data = index->src_buf;\n>  \tref_top = ref_data + index->src_size;\n"},{"id":"326093","messageId":"20170810203612.lt342yq3gnfadjlp@sigill.intra.peff.net","threadId":"46552","inReplyTo":"xmqqmv772nmc.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH V2 1/2] Fix delta integer overflows","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2017-08-10T20:36:12Z","receivedAt":"2017-08-10T20:36:18Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Thu, Aug 10, 2017 at 01:07:07PM -0700, Junio C Hamano wrote:\n\n> Perhaps we should teach the receiving end to notice that the varint\n> data it reads encodes a size that is too large for it to grok and\n> die.  With that, we can safely move forward with whatever size_t\n> each platform uses.\n\nYes, this is very important even for \"unsigned long\". I'd worry that\nmalicious input could cause us to wrap to 0, and we'd potentially write\ninto a too-small buffer[1].\n\nThere's some prior art with checking this against bitsizeof() in\nunpack_object_header_buffer() but get_delta_hdr_size() does not seem to\nhave a check.\n\n-Peff\n\n[1] In most cases it's _probably_ not a vulnerability to wrap here,\n    because we'd just read less data than we ought to. But it makes me\n    nervous nonetheless.\n"},{"id":"326108","messageId":"xmqqwp6b13lo.fsf@gitster.mtv.corp.google.com","threadId":"46552","inReplyTo":"1502348462-4992-2-git-send-email-martin@mail.zuhause","subject":"Re: [PATCH V2 2/2] Convert size datatype to size_t","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2017-08-10T22:04:51Z","receivedAt":"2017-08-10T22:05:21Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Martin Koegler <martin.koegler@chello.at> writes:\n\n> For next. As this touches core functions, it will likely produce\n> conflicts with other changes. Please provide the commit you want\n> to rebase the patch on and I'll produce a V3.\n\nNo matter what base you pick, by the time the series is merged with\nother topics in flight to form an updated 'pu' branch, any series of\nthis invasiveness will cause conflict.  \n\nSo from that point of view, picking 'master' or 'next' as the base\nwould not make much difference.\n\nHowever, picking 'next' (or 'pu') as the base is definitely worse\nthan 'master' for a different reason.  Anything based on 'next',\neven though it may apply cleanly there, will not be able to graduate\nto 'master' without dragging all the other topics that are in 'next'\nwith it.  Immediately after a feature release is the worst time, as\nwe will rewind and rebuild 'next' on top of 'master'.\n\nIn practice, the only sensible base for an invasive change is the\nmimimum one you create yourself.  You would:\n\n (1) Start from a reasonably stable base, like 'master'.\n\n (2) Among topics that are in flight but not in 'master', find the\n     ones that materially interfere with your changes.  Merge them\n     on top of (1).\n\n (3) Then build your change on top.\n\nIn the patch series you create in step 3, you would note which base\nyou chosen (e.g. \"v2.14.1\") in step 1, plus the names of the topics\nyou merged in step 2, after three-dash lines.\n\nThe set of topics you find in step 2 might end up including a topic\nthat is of dubious doneness (e.g. especially the ones that are not\nyet in 'next').  In such a case, you or the other topic may have to\nyield and wait for the other to stabilize.  Git is not a substitute\nfor inter-developer communication, and you'd talk to the author of\nthe other topic and coordinate between yourselves when it happens.\n\nThanks.\n\n"},{"id":"326132","messageId":"20170811071244.GC15128@mail.zuhause","threadId":"46552","inReplyTo":"xmqqwp6b13lo.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH V2 2/2] Convert size datatype to size_t","fromName":"Martin Koegler","fromEmail":"martin.koegler@chello.at","sentAt":"2017-08-11T07:12:44Z","receivedAt":"2017-08-11T07:12:52Z","isPatch":true,"sender":{"key":"martin.koegler@chello.at","avatar":"https://avatars.githubusercontent.com/u/1859030?v=4"},"body":"On Thu, Aug 10, 2017 at 03:04:51PM -0700, Junio C Hamano wrote:\n> Martin Koegler <martin.koegler@chello.at> writes:\n> \n> > For next. As this touches core functions, it will likely produce\n> > conflicts with other changes. Please provide the commit you want\n> > to rebase the patch on and I'll produce a V3.\n> \n> No matter what base you pick, by the time the series is merged with\n> other topics in flight to form an updated 'pu' branch, any series of\n> this invasiveness will cause conflict.  \n\nI don't have time to track all changes floating around on the git mailing list.\n\nIf you want the some of my patches based on a specific set of commits, please ask for it.\nI'll try to rebase my patche(s) on it.\n\nRegards,\nMartin\n"},{"id":"326133","messageId":"20170811074341.GD15128@mail.zuhause","threadId":"46552","inReplyTo":"xmqqmv772nmc.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH V2 1/2] Fix delta integer overflows","fromName":"Martin Koegler","fromEmail":"martin.koegler@chello.at","sentAt":"2017-08-11T07:43:41Z","receivedAt":"2017-08-11T07:43:59Z","isPatch":true,"sender":{"key":"martin.koegler@chello.at","avatar":"https://avatars.githubusercontent.com/u/1859030?v=4"},"body":"On Thu, Aug 10, 2017 at 01:07:07PM -0700, Junio C Hamano wrote:\n> > The current delta code produces incorrect pack objects for files > 4GB.\n> >\n> > Signed-off-by: Martin Koegler <martin.koegler@chello.at>\n> \n> I am a bit torn on this change.\n> \n> Given that this is not merely a local storage format but it also is\n> an interchange format, we would probably want to make sure that the\n> receiving end (e.g. get_delta_hdr_size() that is used at the\n> beginning of patch_delta()) on a platform whose size_t is smaller\n> than that of a platform that produced the delta stream with this\n> code behaves \"sensibly\".\n\nOverflows would already be detected during unpack:\n* Assuming size_t = uint32, the system should just be able to handle up to 4GB of process memory.\nSo loading any source blob larger than 4GB should already fail.\n* Assuming size_t = uint32 and a source blob size < 4 GB, the target blob size would be readed\ntruncated. apply_delta checks, that the generated result matches the encoded size - this check would\nfail.\n \n> If we replaced ulong we use in create/patch delta codepaths with\n> uint32_t, that would be safer, just because the encoder would not be\n> able to emit varint that is larger than the receivers to handle.\n> But that defeats the whole point of using varint() to encode the\n> sizes in the first place.  It was partly done for space saving, but\n> more for allowing larger sizes and larger ulong in the future\n> without having to change the file format.\n\nThe ondisk-format is able to handle larger sizes [using a slightly worse compression].\nThe current implementation is just buggy.\n\nI would not move to uint32_t. The remaing part of git uses \"unsigned long\", so the \ndelta code could still be called with larger files.\n\nWe will also see more RAM as well as CPU power - reducing the limits just because of older plattforms,\nwhich can't even handle such large blobs, is the wrong way.\n\nRegards,\nMartin\n"},{"id":"326157","messageId":"xmqq378y0wy0.fsf@gitster.mtv.corp.google.com","threadId":"46552","inReplyTo":"20170811074341.GD15128@mail.zuhause","subject":"Re: [PATCH V2 1/2] Fix delta integer overflows","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2017-08-11T18:40:55Z","receivedAt":"2017-08-11T18:41:08Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Martin Koegler <martin.koegler@chello.at> writes:\n\n> On Thu, Aug 10, 2017 at 01:07:07PM -0700, Junio C Hamano wrote:\n>> > The current delta code produces incorrect pack objects for files > 4GB.\n>> >\n>> > Signed-off-by: Martin Koegler <martin.koegler@chello.at>\n>> \n>> I am a bit torn on this change.\n>> \n>> Given that this is not merely a local storage format but it also is\n>> an interchange format, we would probably want to make sure that the\n>> receiving end (e.g. get_delta_hdr_size() that is used at the\n>> beginning of patch_delta()) on a platform whose size_t is smaller\n>> than that of a platform that produced the delta stream with this\n>> code behaves \"sensibly\".\n>\n> Overflows would already be detected during unpack:\n> * Assuming size_t = uint32, the system should just be able to handle up to 4GB of process memory.\n> So loading any source blob larger than 4GB should already fail.\n\nAfter re-reading patch_delta(), I agree.  The loading of the base\nobject (i.e. the procedure to prepare src_buf & src_size fed to the\nfunction) would have failed already.\n\n> * Assuming size_t = uint32 and a source blob size < 4 GB, the\n> target blob size would be readed truncated. apply_delta checks,\n> that the generated result matches the encoded size - this check\n> would fail.\n\nWhen target size is truncated, we allocate much less than the real\ntarget size, and start reading the input.  But patch_delta() makes\nsure that what is copied into the buffer, either by copying bytes\nliterally from the pack data or by copying from src_buf, will never\ncause the resulting dst_buf overflow (especially after your change\nupdates \"size\" to size_t), so we should be safe on this side, too.\n>  \n>> If we replaced ulong we use in create/patch delta codepaths with\n>> uint32_t, that would be safer, just because the encoder would not be\n>> able to emit varint that is larger than the receivers to handle.\n>> But that defeats the whole point of using varint() to encode the\n>> sizes in the first place.  It was partly done for space saving, but\n>> more for allowing larger sizes and larger ulong in the future\n>> without having to change the file format.\n>\n> The ondisk-format is able to handle larger sizes [using a slightly worse compression].\n> The current implementation is just buggy.\n>\n> I would not move to uint32_t. The remaing part of git uses \"unsigned long\", so the \n> delta code could still be called with larger files.\n\nOh, absolutely.  I was merely commenting on the lack of any error\nchecking in get_delta_hdr_size() helper function, and dismissing a\nnaive move to limiting the file format by insisting on uint32_t as\nan unworkable workaround.\n"},{"id":"326158","messageId":"xmqqy3qqymgm.fsf@gitster.mtv.corp.google.com","threadId":"46552","inReplyTo":"20170810203612.lt342yq3gnfadjlp@sigill.intra.peff.net","subject":"Re: [PATCH V2 1/2] Fix delta integer overflows","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2017-08-11T18:43:21Z","receivedAt":"2017-08-11T18:43:34Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jeff King <peff@peff.net> writes:\n\n> On Thu, Aug 10, 2017 at 01:07:07PM -0700, Junio C Hamano wrote:\n>\n>> Perhaps we should teach the receiving end to notice that the varint\n>> data it reads encodes a size that is too large for it to grok and\n>> die.  With that, we can safely move forward with whatever size_t\n>> each platform uses.\n>\n> Yes, this is very important even for \"unsigned long\". I'd worry that\n> malicious input could cause us to wrap to 0, and we'd potentially write\n> into a too-small buffer[1].\n>\n> There's some prior art with checking this against bitsizeof() in\n> unpack_object_header_buffer() but get_delta_hdr_size() does not seem to\n> have a check.\n>\n> -Peff\n>\n> [1] In most cases it's _probably_ not a vulnerability to wrap here,\n>     because we'd just read less data than we ought to. But it makes me\n>     nervous nonetheless.\n\nAs I said in my other message in the thread, as long as the callers\nof get_delta_hdr_size() are written correctly, it should be OK.  And\npatch_delta() should be OK, even for \"unsigned long\" when it is too\nsmall.  It just will not produce correct result and instead abort,\nand the patch under discussion fixes that.\n\n\n"}]}