git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] git-compat-util: introduce `count_t` typedef

From
Junio C Hamano <gitster@pobox.com>
Date
Aug 7, 2025, 16:43 UTC
Message-ID
<xmqq4iujqdzm.fsf@gitster.g>
In-Reply-To
<582e8e75-c6eb-4845-8f3b-62f234f0964f@gmail.com>
Phillip Wood <phillip.wood123@gmail.com> writes:
Show 14 quoted lines
> Hi Patrick
>
> On 07/08/2025 10:22, Patrick Steinhardt wrote:
>> Historically, Git has been very lenient with its use of integer types
>> and didn't really give much thought into which type to use in what
>> situation. We interchangeably mix and match signed and unsigned types
>> and often times blindly convert them. This use has led to several
>> out-of-bounds reads and writes in the past, some of which could be
>> turned into arbitrary code execution.
>
> My feeling is that one of the main problems has been using different
> types for loop indexes and loop limits. If we mandated that the loop
> index had to be the same type as the limit that would improve things
> considerably and without mandating a particular type.

Yup. And the limit being unsigned would force the counter to be also unsigned, which can introduce buggy constructs (like counting down).

Show 13 quoted lines
>> A discussion that regularly comes up in this context though is what
>> types to use for counting entities:
>>    - One question is whether the type should be signed or unsigned.
>>      Arguably, the answer should be to use unsigned types as long as we
>>      know that we never need a negative value, e.g. as a sentinel. This
>>      helps guide the reader and explicitly conveys the sense that such a
>>      counter is only ever going to be a non-negative number. Otherwise,
>>      code would need to be more careful as it may hold negative values.
>
> The counter argument to this is that it is easy to write incorrect
> loops when counting down if the loop variable is unsigned. Using a
> typedef that hides the actual type makes that harder to spot as it is
> not immediately obvious whether the loop index is signed or not.

This is very valid argument against typedef for something trivial like an integer. Use of proposed count_t loses both size and signedness information.

Thanks.
Previous: Phillip WoodNext: Junio C Hamano
Message 4 of 6 in “git-compat-util: introduce `count_t` typedef”
  1. git-compat-util: introduce `count_t` typedefPatrick Steinhardt, Aug 7, 2025
  2. Matthias AßhauerAug 7, 2025
  3. Phillip WoodAug 7, 2025
  4. Junio C HamanoAug 7, 2025
  5. Junio C HamanoAug 7, 2025
  6. Taylor BlauAug 7, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.