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

Re: [PATCH v4] utf8: replace utf8_strwidth todo with descriptive comment

From
Junio C Hamano <gitster@pobox.com>
Date
Jul 28, 2026, 18:24 UTC
Message-ID
<xmqq33x3dlbw.fsf@gitster.g>
In-Reply-To
<c8fb2eba-c1c8-4f59-b467-e6d4766623d8@gmail.com>
Phillip Wood <phillip.wood123@gmail.com> writes:
Show 5 quoted lines
> I don't think this comment, or the lines below add anything useful to 
> the message. It would be better to say something like
>
> As we do not want to change the return type, update the comment to 
> explain that and the need for the explicit cast.
Perfect ;-)
Show 21 quoted lines
>> diff --git a/utf8.c b/utf8.c
>> index 96460cc..1b55bd4 100644
>> --- a/utf8.c
>> +++ b/utf8.c
>> @@ -227,8 +227,9 @@ int utf8_strnwidth(const char *string, size_t len, int skip_ansi)
>>   	}
>>   
>>   	/*
>> -	 * TODO: fix the interface of this function and `utf8_strwidth()` to
>> -	 * return `size_t` instead of `int`.
>> +	 * The function is used in multiple locations where the callers
>> +	 * expect the result to be a signed int value. We cast the
>> +	 * result to an int to avoid changing signatures of all callers.
>
> The last sentence does not really capture the reasons given in the 
> message of the commit that added this comment. If you haven't done so 
> already you should read it - see 937b71cc8b (utf8: fix overflow when 
> returning string width, 2022-12-01). The fundamental reason to call 
> cast_size_t_to_int(), rather than relying on an implicit conversion to 
> the return type, is not about changing signatures, it is about avoiding 
> an overflow that caused git to crash.

The comment should also answer why the callers want an int, and whether that is a legitimate need. Topics the comment may want to cover include:

   - Callers want display width; we will never deal with output
     wider than 2 billion columns, so int is adequate, provided we
     do not cause bugs due to integer wraparound.
   - The return value is used to compute width in constructs like:
        printf("%*s", width, string)
     which requires int, not size_t.  Instead of forcing these
     callers to call cast_size_t_to_int() individually, this
     function should return int after ensuring the value is correct
     without wraparound.

This is in addition to explaining why we want cast_size_t_to_int(), as you described above.

> When you send a new version of the patch please CC everyone who 
> commented on previous versions so they don't have to trawl the list to 
> find it.
Thanks.
Previous: Hardik KumarNext: Hardik Kumar
Message 22 of 23 in “change utf8_strwidth() return type to size_t”
  1. change utf8_strwidth() return type to size_tHardik Kumar, Jul 26, 2026
  2. René ScharfeJul 26, 2026
  3. Hardik KumarJul 26, 2026
  4. Pablo SabaterJul 26, 2026
  5. Hardik KumarJul 26, 2026
  6. utf8: use size_t for string width methods and callee sites.Hardik Kumar, Jul 26, 2026
  7. Junio C HamanoJul 27, 2026
  8. Junio C HamanoJul 27, 2026
  9. Hardik KumarJul 27, 2026
  10. Pablo SabaterJul 27, 2026
  11. Hardik KumarJul 27, 2026
  12. utf8: make utf8_strwidth() and utf8_strnwidth() return size_tHardik Kumar, Jul 27, 2026
  13. Hardik KumarJul 27, 2026
  14. Phillip WoodJul 27, 2026
  15. Junio C HamanoJul 27, 2026
  16. Hardik KumarJul 27, 2026
  17. Junio C HamanoJul 27, 2026
  18. Hardik KumarJul 27, 2026
  19. utf8: replace utf8_strwidth todo with descriptive commentHardik Kumar, Jul 27, 2026
  20. Phillip WoodJul 28, 2026
  21. Hardik KumarJul 28, 2026
  22. Junio C HamanoJul 28, 2026
  23. utf8: replace utf8_strwidth todo with descriptive commentHardik Kumar, Jul 28, 2026

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.