[PATCH 2/3] odb: widen odb_write_object() length to size_t
- From
Christian Couder <christian.couder@gmail.com>
- Date
- Nov 17, 2026, 17:43 UTC
- Message-ID
- <20261117174353.1158905-7-christian.couder@gmail.com>
- In-Reply-To
- <20261117174353.1158905-5-christian.couder@gmail.com>
odb_write_object() and odb_write_object_ext() use an `unsigned long` for their object length argument, while almost everything they pass it to already uses `size_t`: hash_object_file(), format_object_header(), convert_object_file(), the `write_object` callback of `struct odb_source`, and `git_zstream`.
Many callers also pass a `size_t`, often a `strbuf.len`. On LLP64 platforms like 64-bit Windows, where `unsigned long` is 32 bits, such lengths may silently be truncated.
Since 81db32ddce (Merge branch 'jk/xdiff-size_t', 2026-10-15), the size of an ll_merge() result is also passed as a `size_t` to odb_write_object(). It cannot be truncated as since 1c93cc03ea (Merge branch 'kn/safer-mem-arithmetic-in-xdiff', 2026-10-16), that size argument cannot be greater than LONG_MAX because xdl_do_merge() errors out if `size > LONG_MAX`.
But we want to get rid of that `size > LONG_MAX` check in a following commit.
To fix the possible truncation issues with many callers and to prepare for getting rid of the check, let's use `size_t` for:
- the length argument of odb_write_object(), odb_write_object_ext(), the odb_source_write_object() wrapper, and write_loose_object(),
- the size stored in `struct inmemory_object`, which is later copied back into `size_t` fields (`object_info.sizep` and `odb_stream.size`).
Signed-off-by: Christian Couder <christian.couder@gmail.com> --- odb.c | 2 +- odb.h | 4 ++-- odb/source-inmemory.c | 2 +- odb/source-loose.c | 2 +- odb/source.h | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-)
diff --git a/odb.c b/odb.c index de5be285d3..56a7910b03 100644 --- a/odb.c +++ b/odb.c @@ -702,7 +702,7 @@ void odb_assert_oid_type(struct object_database *odb, } int odb_write_object_ext(struct object_database *odb, - const void *buf, unsigned long len, + const void *buf, size_t len, enum object_type type, struct object_id *oid, const struct object_id *compat_oid_in, diff --git a/odb.h b/odb.h index 857637f980..a09748bdc2 100644 --- a/odb.h +++ b/odb.h @@ -634,14 +634,14 @@ enum odb_write_object_flags { * Returns 0 on success, a negative error code otherwise. */ int odb_write_object_ext(struct object_database *odb, - const void *buf, unsigned long len, + const void *buf, size_t len, enum object_type type, struct object_id *oid, const struct object_id *compat_oid, enum odb_write_object_flags flags); static inline int odb_write_object(struct object_database *odb, - const void *buf, unsigned long len, + const void *buf, size_t len, enum object_type type, struct object_id *oid) { diff --git a/odb/source-inmemory.c b/odb/source-inmemory.c index 75cf29fa9d..c13cffc805 100644 --- a/odb/source-inmemory.c +++ b/odb/source-inmemory.c @@ -10,7 +10,7 @@ struct inmemory_object { enum object_type type; const void *buf; - unsigned long size; + size_t size; }; static const struct inmemory_object *find_cached_object(struct odb_source_inmemory *source, diff --git a/odb/source-loose.c b/odb/source-loose.c index 50cb388c35..60801e70e8 100644 --- a/odb/source-loose.c +++ b/odb/source-loose.c @@ -758,7 +758,7 @@ static int end_loose_object_common(struct odb_source_loose *loose, static int write_loose_object(struct odb_source_loose *loose, const struct object_id *oid, char *hdr, - int hdrlen, const void *buf, unsigned long len, + int hdrlen, const void *buf, size_t len, const time_t *mtime, unsigned flags) { int fd, ret; diff --git a/odb/source.h b/odb/source.h index 7b56f51b82..8daf5df31c 100644 --- a/odb/source.h +++ b/odb/source.h @@ -454,7 +454,7 @@ static inline int odb_source_freshen_object(struct odb_source *source, * object ID and the compatibility object ID, if non-NULL. */ static inline int odb_source_write_object(struct odb_source *source, - const void *buf, unsigned long len, + const void *buf, size_t len, enum object_type type, const struct object_id *oid, const struct object_id *compat_oid,
-- 2.56.0