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

[PATCH] revision: parse "git log -<count>" more carefully

From
Junio C Hamano <gitster@pobox.com>
Date
Jun 6, 2014, 22:33 UTC
Message-ID
<xmqqk38tpt1m.fsf@gitster.dls.corp.google.com>

This mistyped command line simply ignores "master" and ends up showing two commits from the current HEAD:

    $ git log -2master

because we feed "2master" to atoi() without making sure that the whole string is parsed as an integer.

Use the strtol_i() helper function instead.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 * We may want to wage a war on atoi(), which we sort-of did back
   when strtol_i() and strtoul_ui() helpers were introduced.  Also I
   suspect that many call sites of strtol() and strtoul() may become
   simpler if they are updated to use these helper functions.  Hint,
   hint...
 revision.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/revision.c b/revision.c
index 71e2337..f6a1088 100644
--- a/revision.c
+++ b/revision.c
@@ -1648,8 +1648,10 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
 		revs->skip_count = atoi(optarg);
 		return argcount;
 	} else if ((*arg == '-') && isdigit(arg[1])) {
-	/* accept -<digit>, like traditional "head" */
-		revs->max_count = atoi(arg + 1);
+		/* accept -<digit>, like traditional "head" */
+		if (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||
+		    revs->max_count < 0)
+			die("'%s': not a non-negative integer", arg + 1);
 		revs->no_walk = 0;
 	} else if (!strcmp(arg, "-n")) {
 		if (argc <= 1)
Message 1 of 1 in “revision: parse "git log -<count>" more carefully”
  1. revision: parse "git log -<count>" more carefullyJunio C Hamano, Jun 6, 2014

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.