Subject: Re: [PATCH v2 2/4] string-list: replace negative index encoding with "exact_match" parameter

From: Junio C Hamano <[email-masked]>
Date: Thu, 25 Sep 2025 06:33:13 -0700

Jeff King <[email-masked]> writes:

> Yes, but it's much harder to wrap a size_t, especially if the code is
> allocating as it goes (e.g., a loop expanding an array). Because if
> expanding your allocation from "n" to "n+k" items will overflow, then
> that implies the current allocation is within "k" items of filling up
> the entire memory space.

We'd be protecting ourselves by noticing that n+k wraps around with
st_add() and friends, and relying on malloc() and realloc() to
notice and signal an error.  Use of size_t to count the number of
things that are getting allocated is not making these any easier to
do compared to the case you were counting in "int", no?  Either way
we'd need to be careful.

> But if we use size_t inside string_list, say, and you do this:
>
>    for (int i = 0; i < list.nr; i++)
> 	printf("got: %s", list->items[i].string);
>
> Now we have another problem.

It is obvious that in order to count up from 0 to list.nr, you'd
better use a counter variable of the typeof(list.nr) or manage the
wraparound yourself.  So I do not see what is new here.  My point
was that use of size_t to _count_ the strings in the string list is
where the problem stems from.

So, I still do not buy into the religion or superstition that things
must be counted in size_t yet.