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

Re: [PATCH v3] diff: check range before dereferencing an array element

From
Jeff King <peff@peff.net>
Date
Apr 29, 2025, 21:58 UTC
Message-ID
<20250429215847.GB36727@coredump.intra.peff.net>
In-Reply-To
<pull.1887.v3.git.1745926679028.gitgitgadget@gmail.com>
On Tue, Apr 29, 2025 at 11:37:58AM +0000, Johannes Schindelin via GitGitGadget wrote:
Show 8 quoted lines
> This CodeQL rule unfortunately is also triggered by the
> `fill_es_indent_data()` code, even though the condition `off < len - 1`
> does not even need to guarantee that the offset is in bounds (`s` points
> to a NUL-terminated string, for which `s[off] == '\r'` would fail before
> running out of bounds).
> 
> Let's work around this rare false positive to help us use an otherwise
> mostly useful tool is a worthy thing to do.

Since this is marked as fixing a false positive, and since it presumably _does_ fix the false positive in practice, this is OK with me.

But...
Show 9 quoted lines
> --- a/diff.c
> +++ b/diff.c
> @@ -892,7 +892,7 @@ static void fill_es_indent_data(struct emitted_diff_symbol *es)
>  
>  	/* skip any \v \f \r at start of indentation */
>  	while (s[off] == '\f' || s[off] == '\v' ||
> -	       (s[off] == '\r' && off < len - 1))
> +	       (off < len - 1 && s[off] == '\r'))
>  		off++;

...since the same pattern exists for the other s[off] checks, is it worth future-proofing this like:

  while (off < len - 1 &&
         (s[off] == '\f' || s[off] == '\v' || s[off] == '\r')
?

I say "future proofing" because we don't know whether future versions of CodeQL might complain about them. Presumably it does not yet because it isn't smart enough to look outside the parenthesized &&-condition. But if reading s[len] would be a problem for the '\r' check, it would be for the others as well.

-Peff
Previous: Junio C HamanoNext: Junio C Hamano
Message 15 of 18 in “Range-check array index before access”
  1. 0/2 Range-check array index before accessJohannes Schindelin via GitGitGadget, Mar 26, 2025
  2. 1/2 diff: check range before dereferencing an array elementJohannes Schindelin via GitGitGadget, Mar 26, 2025
  3. Junio C HamanoMar 27, 2025
  4. Phillip WoodMar 27, 2025
  5. 2/2 read-cache: check range before dereferencing an array elementJohannes Schindelin via GitGitGadget, Mar 26, 2025
  6. Jeff KingMar 26, 2025
  7. 0/2 Range-check array index before accessJohannes Schindelin via GitGitGadget, Mar 27, 2025
  8. 1/2 diff: check range before dereferencing an array elementJohannes Schindelin via GitGitGadget, Mar 27, 2025
  9. Junio C HamanoMar 28, 2025
  10. 2/2 read-cache: check range before dereferencing an array elementJohannes Schindelin via GitGitGadget, Mar 27, 2025
  11. Jeff KingMar 28, 2025
  12. Junio C HamanoApr 11, 2025
  13. diff: check range before dereferencing an array elementJohannes Schindelin via GitGitGadget, Apr 29, 2025
  14. Junio C HamanoApr 29, 2025
  15. Jeff KingApr 29, 2025
  16. Junio C HamanoApr 29, 2025
  17. Jeff KingApr 29, 2025
  18. Junio C HamanoApr 29, 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.