{"thread":{"id":"36863","subject":"[PATCH] revision: parse \"git log -<count>\" more carefully","startedAt":"2014-06-06T22:33:25Z","lastAt":"2014-06-06T22:33:25Z","messageCount":1,"participants":["Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"243567","messageId":"xmqqk38tpt1m.fsf@gitster.dls.corp.google.com","threadId":"36863","inReplyTo":null,"subject":"[PATCH] revision: parse \"git log -<count>\" more carefully","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2014-06-06T22:33:25Z","receivedAt":"2014-06-06T22:33:25Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"This mistyped command line simply ignores \"master\" and ends up\nshowing two commits from the current HEAD:\n\n    $ git log -2master\n\nbecause we feed \"2master\" to atoi() without making sure that the\nwhole string is parsed as an integer.\n\nUse the strtol_i() helper function instead.\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n\n * We may want to wage a war on atoi(), which we sort-of did back\n   when strtol_i() and strtoul_ui() helpers were introduced.  Also I\n   suspect that many call sites of strtol() and strtoul() may become\n   simpler if they are updated to use these helper functions.  Hint,\n   hint...\n\n revision.c | 6 ++++--\n 1 file changed, 4 insertions(+), 2 deletions(-)\n\ndiff --git a/revision.c b/revision.c\nindex 71e2337..f6a1088 100644\n--- a/revision.c\n+++ b/revision.c\n@@ -1648,8 +1648,10 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg\n \t\trevs->skip_count = atoi(optarg);\n \t\treturn argcount;\n \t} else if ((*arg == '-') && isdigit(arg[1])) {\n-\t/* accept -<digit>, like traditional \"head\" */\n-\t\trevs->max_count = atoi(arg + 1);\n+\t\t/* accept -<digit>, like traditional \"head\" */\n+\t\tif (strtol_i(arg + 1, 10, &revs->max_count) < 0 ||\n+\t\t    revs->max_count < 0)\n+\t\t\tdie(\"'%s': not a non-negative integer\", arg + 1);\n \t\trevs->no_walk = 0;\n \t} else if (!strcmp(arg, \"-n\")) {\n \t\tif (argc <= 1)\n"}]}