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

[PATCH v6 0/2] Replace atoi() with strtoi_with_tail()

From
Mohit Marathe via GitGitGadget <gitgitgadget@gmail.com>
Date
Feb 4, 2024, 05:48 UTC
Message-ID
<pull.1646.v6.git.1707025718.gitgitgadget@gmail.com>
In-Reply-To
<pull.1646.v5.git.1706416952.gitgitgadget@gmail.com>
Hello,

This patch series replaces atoi() with an updated version of strtol_i() called strtoi_with_tail (Credits: Junio C Hamano). The reasoning behind this is to improve error handling by not allowing non-numerical characters in the hunk header (which might happen in case of a corrupt patch, although rarely).

There is still a change to be made, as Junio says: "A corrupt patch may be getting a nonsense patch-ID with the current code and hopefully is not matching other patches that are not corrupt, but with such a change, a corrupt patch may not be getting any patch-ID and a loop that computes patch-ID for many files and try to match them up might need to be rewritten to take the new failure case into account." I'm not sure where this change needs to me made (maybe get_one_patchid()?). It would be great if anyone could point me to the correct place.

Thanks, Mohit Marathe
Mohit Marathe (2):
  git-compat-util: add strtoi_with_tail()
  patch-id: replace `atoi()` with `strtoi_with_tail`
 builtin/patch-id.c | 12 ++++++++----
 git-compat-util.h  | 18 ++++++++++++++----
 2 files changed, 22 insertions(+), 8 deletions(-)
base-commit: 2a540e432fe5dff3cfa9d3bf7ca56db2ad12ebb9
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1646%2Fmohit-marathe%2Fupdate-strtol_i-v6
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1646/mohit-marathe/update-strtol_i-v6
Pull-Request: https://github.com/gitgitgadget/git/pull/1646
Range-diff vs v5:
 1:  f09b0838f04 ! 1:  98e516a7be7 git-compat-util: add strtoi_with_tail()
     @@ Commit message
          Signed-off-by: Mohit Marathe <mohitmarathe@proton.me>
      
       ## git-compat-util.h ##
     -@@ git-compat-util.h: static inline int strtol_i(char const *s, int base, int *result)
     +@@ git-compat-util.h: static inline int strtoul_ui(char const *s, int base, unsigned int *result)
       	return 0;
       }
       
     +-static inline int strtol_i(char const *s, int base, int *result)
      +#define strtol_i(s,b,r) strtoi_with_tail((s), (b), (r), NULL)
      +static inline int strtoi_with_tail(char const *s, int base, int *result, char **endp)
     -+{
     -+	long ul;
     + {
     + 	long ul;
     +-	char *p;
      +	char *dummy = NULL;
     -+
     + 
      +	if (!endp)
      +		endp = &dummy;
     -+	errno = 0;
     + 	errno = 0;
     +-	ul = strtol(s, &p, base);
     +-	if (errno || *p || p == s || (int) ul != ul)
      +	ul = strtol(s, endp, base);
      +	if (errno ||
      +	    /*
     @@ git-compat-util.h: static inline int strtol_i(char const *s, int base, int *resu
      +	     */
      +	   (dummy && *dummy) ||
      +	    *endp == s || (int) ul != ul)
     -+		return -1;
     -+	*result = ul;
     -+	return 0;
     -+}
     -+
     - void git_stable_qsort(void *base, size_t nmemb, size_t size,
     - 		      int(*compar)(const void *, const void *));
     - #ifdef INTERNAL_QSORT
     + 		return -1;
     + 	*result = ul;
     + 	return 0;
 2:  ee8f4ae991d = 2:  858d6f94e79 patch-id: replace `atoi()` with `strtoi_with_tail`
-- 
gitgitgadget
Previous: Mohit Marathe via GitGitGadgetNext: Mohit Marathe via GitGitGadget
Message 22 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.