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

Re: [PATCH] revision: parse integer arguments to --max-count, --skip, etc., more carefully

From
Jeff King <peff@peff.net>
Date
Dec 12, 2023, 01:30 UTC
Message-ID
<20231212013045.GE376323@coredump.intra.peff.net>
In-Reply-To
<xmqq5y181fx0.fsf_-_@gitster.g>
On Sat, Dec 09, 2023 at 07:35:23AM +0900, Junio C Hamano wrote:
Show 16 quoted lines
> The "rev-list" and other commands in the "log" family, being the
> oldest part of the system, use their own custom argument parsers,
> and integer values of some options are parsed with atoi(), which
> allows a non-digit after the number (e.g., "1q") to be silently
> ignored.  As a natural consequence, an argument that does not begin
> with a digit (e.g., "q") silently becomes zero, too.
> 
> Switch to use strtol_i() and parse_timestamp() appropriately to
> catch bogus input.
> 
> Note that one may naïvely expect that --max-count, --skip, etc., to
> only take non-negative values, but we must allow them to also take
> negative values, as an escape hatch to countermand a limit set by an
> earlier option on the command line; the underlying variables are
> initialized to (-1) and "--max-count=-1", for example, is a
> legitimate way to reinitialize the limit.
This all looks pretty reasonable to me.

I couldn't help but think, though, that surely we have some helpers for this already? But the closest seems to be git_parse_int(), which also allows unit factors. I'm not sure if allowing "-n 1k" would be a feature or a bug. ;)

I guess "strtol_i()" maybe is that helper already, though I did not even know it existed. Looks like it goes back to 2007, and is seldom used. I wonder if there are more spots that could benefit.

I don't think there is any such helper for timestamps, but the checks in your parser look good (strtol_i() checks for overflow as we cast to int, but I don't think we need to do the same here since timestamp_t and parse_timestamp() should be matched).

-Peff
Previous: Junio C HamanoNext: Junio C Hamano
Message 4 of 6 in “[BUG] rev-list doesn't validate arguments to -n option”
  1. Britton KerinDec 7, 2023
  2. Junio C HamanoDec 8, 2023
  3. revision: parse integer arguments to --max-count, --skip, etc., more carefullyJunio C Hamano, Dec 8, 2023
  4. Jeff KingDec 12, 2023
  5. Junio C HamanoDec 12, 2023
  6. Jeff KingDec 12, 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.