From: Elia Pinto Date: Sat, 31 Oct 2026 10:16:35 GMT Subject: [PATCH v3 4/4] convert: use size_t for text statistics and ident counts Message-ID: <153463b531b03e942247c0f6f9b3d89b8e55ac7c.1793439137.git.gitter.spiros@gmail.com> In-Reply-To: gather_stats() and count_ident() use "unsigned" and "int" counters to count characters and markers in buffers whose sizes are measured in size_t. On platforms where size_t can exceed 32 bits, large inputs can overflow these counters. For example, a file with exactly 2^32 lone LFs wraps lonelf back to zero, causing will_convert_lf_to_crlf() to miss the conversion. Switch the counters to size_t and check the size hint for overflow. Triggering this requires processing over 4GiB of input, so no test is added. Suggested-by: Patrick Steinhardt Signed-off-by: Elia Pinto --- convert.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/convert.c b/convert.c index ca5594cab0..34fb4ce685 100644 --- a/convert.c +++ b/convert.c @@ -36,10 +36,10 @@ struct text_stat { /* NUL, CR, LF and CRLF counts */ - unsigned nul, lonecr, lonelf, crlf; + size_t nul, lonecr, lonelf, crlf; /* These are just approximations! */ - unsigned printable, nonprintable; + size_t printable, nonprintable; }; static void gather_stats(const char *buf, size_t size, struct text_stat *stats) @@ -1077,12 +1077,12 @@ static int read_convert_config(const char *var, const char *value, return 0; } -static int count_ident(const char *cp, size_t size) +static size_t count_ident(const char *cp, size_t size) { /* * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$" */ - int cnt = 0; + size_t cnt = 0; char ch; while (size) { @@ -1170,7 +1170,7 @@ static int ident_to_worktree(const char *src, size_t len, struct object_id oid; char *to_free = NULL; const char *dollar, *spc; - int cnt; + size_t cnt; if (!ident) return 0; @@ -1184,7 +1184,7 @@ static int ident_to_worktree(const char *src, size_t len, to_free = strbuf_detach(buf, NULL); hash_object_file(the_hash_algo, src, len, OBJ_BLOB, &oid); - strbuf_grow(buf, len + cnt * (the_hash_algo->hexsz + 3)); + strbuf_grow(buf, st_add(len, st_mult(cnt, the_hash_algo->hexsz + 3))); for (;;) { /* step 1: run to the next '$' */ dollar = memchr(src, '$', len); -- 2.53.0