]> git.ipfire.org Git - thirdparty/git.git/commitdiff
delta: widen `create_delta()` and `diff_delta()` to `size_t`
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Thu, 9 Jul 2026 16:49:33 +0000 (16:49 +0000)
committerJunio C Hamano <gitster@pobox.com>
Thu, 9 Jul 2026 21:55:23 +0000 (14:55 -0700)
Last stop in the delta-encoding API widening for >4 GiB blobs on
Windows: with `create_delta_index()` done in the prior commit and
`create_delta()`/`diff_delta()` finished here, every byte count that
crosses delta.h is now `size_t`. The struct fields they store into have
been `size_t` since the diff-delta struct widening.

The API change must move with all callers in the same commit (the build
only passes when every `&delta_size` matches the new `size_t*`). Caller
updates are kept minimal:

  * builtin/pack-objects.c `get_delta()` and `try_delta()`: widen only
    the local `delta_size` variable; the surrounding unsigned-long
    locals and their `cast_size_t_to_ulong()` shims are out of scope
    here and will be cleaned up in their own commits.

  * builtin/fast-import.c, diff.c, t/helper/test-pack-deltas.c:
    keep the local unsigned-long delta size (each feeds a still-
    unsigned-long downstream consumer: zlib's `avail_in`,
    `deflate_it()`, the test helper's own `do_compress()`), and bridge
    via a temporary `size_t` plus `cast_size_t_to_ulong()`. The new
    casts are paid back in later topics that widen those consumers.

  * t/helper/test-delta.c: widen the local outright (no downstream
    consumer beyond the test's own `out_size`, which is already
    `size_t`).

Note that GCC struggles a bit to figure out that `deltalen` is always
initialized before it is used; To help it along, we initialize it to 0.
This work-around will go away in a later patch series when `deltalen`
can be widened to `size_t`.

Assisted-by: Opus 4.7
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/fast-import.c
builtin/pack-objects.c
delta.h
diff-delta.c
diff.c
t/helper/test-delta.c
t/helper/test-pack-deltas.c

index aa656c5195d3669943d973b226b727e35135c56e..1c6e5366c2ce063251b1f319a617239f68ca1298 100644 (file)
@@ -962,7 +962,7 @@ static int store_object(
        struct object_entry *e;
        unsigned char hdr[96];
        struct object_id oid;
-       unsigned long hdrlen, deltalen;
+       unsigned long hdrlen, deltalen = 0;
        struct git_hash_ctx c;
        git_zstream s;
        struct repo_config_values *cfg = repo_config_values(the_repository);
@@ -998,11 +998,13 @@ static int store_object(
 
        if (last && last->data.len && last->data.buf && last->depth < max_depth
                && dat->len > the_hash_algo->rawsz) {
+               size_t deltalen_st;
 
                delta_count_attempts_by_type[type]++;
                delta = diff_delta(last->data.buf, last->data.len,
                        dat->buf, dat->len,
-                       &deltalen, dat->len - the_hash_algo->rawsz);
+                       &deltalen_st, dat->len - the_hash_algo->rawsz);
+               deltalen = cast_size_t_to_ulong(deltalen_st);
        } else
                delta = NULL;
 
index d0ccf8a62d4e5f6da58ef1295cfdec0330f33bcb..f739fee7532715e62f0a6dc77ad40879fa519346 100644 (file)
@@ -353,7 +353,8 @@ static void index_commit_for_bitmap(struct commit *commit)
 
 static void *get_delta(struct object_entry *entry)
 {
-       unsigned long size, base_size, delta_size;
+       unsigned long size, base_size;
+       size_t delta_size;
        void *buf, *base_buf, *delta_buf;
        enum object_type type;
        size_t size_st = 0, base_size_st = 0;
@@ -2791,7 +2792,8 @@ static int try_delta(struct unpacked *trg, struct unpacked *src,
 {
        struct object_entry *trg_entry = trg->entry;
        struct object_entry *src_entry = src->entry;
-       unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;
+       unsigned long trg_size, src_size, sizediff, max_size, sz;
+       size_t delta_size;
        unsigned ref_depth;
        enum object_type type;
        void *delta_buf;
diff --git a/delta.h b/delta.h
index a19586d789d89b808972f9479e2b9eaccb85bd69..59ccaaa0e0f62505170132bc0e0fe12b4772a988 100644 (file)
--- a/delta.h
+++ b/delta.h
@@ -42,8 +42,8 @@ unsigned long sizeof_delta_index(struct delta_index *index);
  */
 void *
 create_delta(const struct delta_index *index,
-            const void *buf, unsigned long bufsize,
-            unsigned long *delta_size, unsigned long max_delta_size);
+            const void *buf, size_t bufsize,
+            size_t *delta_size, size_t max_delta_size);
 
 /*
  * diff_delta: create a delta from source buffer to target buffer
@@ -54,9 +54,9 @@ create_delta(const struct delta_index *index,
  * updated with its size.  The returned buffer must be freed by the caller.
  */
 static inline void *
-diff_delta(const void *src_buf, unsigned long src_bufsize,
-          const void *trg_buf, unsigned long trg_bufsize,
-          unsigned long *delta_size, unsigned long max_delta_size)
+diff_delta(const void *src_buf, size_t src_bufsize,
+          const void *trg_buf, size_t trg_bufsize,
+          size_t *delta_size, size_t max_delta_size)
 {
        struct delta_index *index = create_delta_index(src_buf, src_bufsize);
        if (index) {
index c93ac425940eae70a1dd00a52db593c093876592..15210e8381e1c7086a3860dc643df29dd37027ca 100644 (file)
@@ -318,8 +318,8 @@ unsigned long sizeof_delta_index(struct delta_index *index)
 
 void *
 create_delta(const struct delta_index *index,
-            const void *trg_buf, unsigned long trg_size,
-            unsigned long *delta_size, unsigned long max_size)
+            const void *trg_buf, size_t trg_size,
+            size_t *delta_size, size_t max_size)
 {
        unsigned int i, val;
        off_t outpos, moff;
diff --git a/diff.c b/diff.c
index 2a9d0d86871139fdc9c23affd2981344f2db98a5..69eb2f76a4e51cec44190ea199bdde107328394d 100644 (file)
--- a/diff.c
+++ b/diff.c
@@ -3647,9 +3647,11 @@ static void emit_binary_diff_body(struct diff_options *o,
        delta = NULL;
        deflated = deflate_it(two->ptr, two->size, &deflate_size);
        if (one->size && two->size) {
+               size_t delta_size_st = 0;
                delta = diff_delta(one->ptr, one->size,
                                   two->ptr, two->size,
-                                  &delta_size, deflate_size);
+                                  &delta_size_st, deflate_size);
+               delta_size = cast_size_t_to_ulong(delta_size_st);
                if (delta) {
                        void *to_free = delta;
                        orig_size = delta_size;
index 8223a60229229e68a14baff02815b8f7dfc11ac2..d807afef751b48a8401e734040bd98e2c8c4d5b3 100644 (file)
@@ -32,7 +32,7 @@ int cmd__delta(int argc, const char **argv)
                die_errno("unable to read '%s'", argv[3]);
 
        if (argv[1][1] == 'd') {
-               unsigned long delta_size;
+               size_t delta_size;
                out_buf = diff_delta(from.buf, from.len,
                                     data.buf, data.len,
                                     &delta_size, 0);
index 840797cf0dbabbef1d3a5f8efd3b43347bfafa34..5e0f72684270030576dd40ee1d5eae9dfc8352ed 100644 (file)
@@ -49,7 +49,7 @@ static void write_ref_delta(struct hashfile *f,
 {
        unsigned char header[MAX_PACK_OBJECT_HEADER];
        unsigned long delta_size, compressed_size, hdrlen;
-       size_t size, base_size;
+       size_t size, base_size, delta_size_st = 0;
        enum object_type type;
        void *base_buf, *delta_buf;
        void *buf = odb_read_object(the_repository->objects,
@@ -65,7 +65,8 @@ static void write_ref_delta(struct hashfile *f,
                die("unable to read %s", oid_to_hex(base));
 
        delta_buf = diff_delta(base_buf, base_size,
-                              buf, size, &delta_size, 0);
+                              buf, size, &delta_size_st, 0);
+       delta_size = cast_size_t_to_ulong(delta_size_st);
 
        compressed_size = do_compress(&delta_buf, delta_size);