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

Re: [PATCH] sideband.c: replace int with size_t for clarity

From
Junio C Hamano <gitster@pobox.com>
Date
Dec 22, 2023, 19:01 UTC
Message-ID
<xmqqedfejc32.fsf@gitster.g>
In-Reply-To
<20231222175747.GA2880@tb-raspi4>
Torsten Bögershausen <tboegi@web.de> writes:
Just this part.
Show 11 quoted lines
> Further down, we read
> 	for (i = 0; i < ARRAY_SIZE(keywords); i++) {
>
> However, a size of an array can never be negative, so that
> an unsigned data type is a better choice than a signed.
> And, arrays can have more elements than an int can address,
> at least in theory.
> For a reader it makes more sense, to replace
> int i;
> with
> size_t i;

It is a very good discipline to use size_t to index into an array whose size is externally controled (e.g., we slurp what the end user or the server on the other end of the connection gave us into a piece of memory we allocate) to avoid integer overflows as "int" is often narrower than "size_t". But this particular one is a Meh; the keywords[] is a small hardcoded array whose size and contents are totally under our control.

Previous: Torsten BögershausenNext: Taylor Blau
Message 5 of 12 in “sideband.c: replace int with size_t for clarity”
  1. sideband.c: replace int with size_t for clarityChandra Pratap via GitGitGadget, Dec 22, 2023
  2. Torsten BögershausenDec 22, 2023
  3. Chandra PratapDec 22, 2023
  4. Torsten BögershausenDec 22, 2023
  5. Junio C HamanoDec 22, 2023
  6. Taylor BlauJan 2, 2024
  7. sideband.c: replace int with size_t for clarityChandra Pratap via GitGitGadget, Dec 23, 2023
  8. sideband.c: remove redundant 'NEEDSWORK' tagChandra Pratap via GitGitGadget, Dec 27, 2023
  9. Junio C HamanoDec 27, 2023
  10. sideband.c: remove redundant 'NEEDSWORK' tagChandra Pratap via GitGitGadget, Dec 28, 2023
  11. Junio C HamanoDec 28, 2023
  12. Junio C HamanoDec 26, 2023

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.