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

Re: [PATCH v4 2/2] patch-id: replace `atoi()` with `strtol_i_updated()`

From
Junio C Hamano <gitster@pobox.com>
Date
Jan 24, 2024, 21:02 UTC
Message-ID
<xmqqttn2bg2o.fsf@gitster.g>
In-Reply-To
<17f2dda4907ec03b0783160c53c4896fd76cb053.1706079304.git.gitgitgadget@gmail.com>
"Mohit Marathe via GitGitGadget" <gitgitgadget@gmail.com> writes:
>  	q = p + 4;
>  	n = strspn(q, digits);
>  	if (q[n] == ',') {
>  		q += n + 1;

So, we saw "@@ -" and skipped over these four bytes, skipped the digits from there, and found a comma.

For "@@ -29,14 +30,18 @@", for example, our q is now "14 +30,18 @@" as we have skipped over that comma after 29.

> -		*p_before = atoi(q);
> +		if (strtol_i_updated(q, 10, p_before, &endp) != 0)
> +			return 0;

We parse out 14 and store it to *p_before. endp points at " +30..." now.

>  		n = strspn(q, digits);
> +		if (endp != q + n)
> +			return 0;

Is this necessary? By asking strtol_i_updated() where the number ended, we already know endp without skipping the digits in q with strspn(). Shouldn't these three lines become more like

		n = endp - q;
instead?  

After all, we are not trying to find a bug in strtol_i_updated(), which would be the only reason how this "return 0" would trigger.

Show 13 quoted lines
>  	} else {
>  		*p_before = 1;
>  	}
> @@ -48,8 +53,11 @@ static int scan_hunk_header(const char *p, int *p_before, int *p_after)
>  	n = strspn(r, digits);
>  	if (r[n] == ',') {
>  		r += n + 1;
> -		*p_after = atoi(r);
> +		if (strtol_i_updated(r, 10, p_after, &endp) != 0)
> +			return 0;
>  		n = strspn(r, digits);
> +		if (endp != r + n)
> +			return 0;
Likewise.
>  	} else {
>  		*p_after = 1;
>  	}
Previous: Mohit Marathe via GitGitGadgetNext: Mohit Marathe
Message 16 of 24 in “Replace atoi() with strtol_i2()”
  1. 0/2 Replace atoi() with strtol_i2()Mohit Marathe via GitGitGadget, Jan 22, 2024
  2. 1/2 git-compat-util: add strtol_i2Mohit Marathe via GitGitGadget, Jan 22, 2024
  3. 2/2 patch-id: replace `atoi()` with `strtol_i2()`Mohit Marathe via GitGitGadget, Jan 22, 2024
  4. Junio C HamanoJan 22, 2024
  5. Mohit MaratheJan 24, 2024
  6. 0/2 Replace atoi() with strtol_i_updated()Mohit Marathe via GitGitGadget, Jan 24, 2024
  7. 1/2 git-compat-util: add strtol_i_updated()Mohit Marathe via GitGitGadget, Jan 24, 2024
  8. 2/2 patch-id: replace `atoi()` with `strtol_i_updated()`Mohit Marathe via GitGitGadget, Jan 24, 2024
  9. 0/2 Replace atoi() with strtol_i_updated()Mohit Marathe via GitGitGadget, Jan 24, 2024
  10. 1/2 git-compat-util: add strtol_i_updated()Mohit Marathe via GitGitGadget, Jan 24, 2024
  11. 2/2 patch-id: replace `atoi()` with `strtol_i_updated()`Mohit Marathe via GitGitGadget, Jan 24, 2024
  12. 0/2 Replace atoi() with strtol_i_updated()Mohit Marathe via GitGitGadget, Jan 24, 2024
  13. 1/2 git-compat-util: add strtol_i_updated()Mohit Marathe via GitGitGadget, Jan 24, 2024
  14. Junio C HamanoJan 24, 2024
  15. 2/2 patch-id: replace `atoi()` with `strtol_i_updated()`Mohit Marathe via GitGitGadget, Jan 24, 2024
  16. Junio C HamanoJan 24, 2024
  17. Mohit MaratheJan 28, 2024
  18. 0/2 Replace atoi() with strtoi_with_tail()Mohit Marathe via GitGitGadget, Jan 28, 2024
  19. 1/2 git-compat-util: add strtoi_with_tail()Mohit Marathe via GitGitGadget, Jan 28, 2024
  20. Junio C HamanoJan 30, 2024
  21. 2/2 patch-id: replace `atoi()` with `strtoi_with_tail`Mohit Marathe via GitGitGadget, Jan 28, 2024
  22. 0/2 Replace atoi() with strtoi_with_tail()Mohit Marathe via GitGitGadget, Feb 4, 2024
  23. 1/2 git-compat-util: add strtoi_with_tail()Mohit Marathe via GitGitGadget, Feb 4, 2024
  24. 2/2 patch-id: replace `atoi()` with `strtoi_with_tail`Mohit Marathe via GitGitGadget, Feb 4, 2024

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.