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

[PATCH v2 1/2] git-compat-util: add strtol_i_updated()

From
Mohit Marathe via GitGitGadget <gitgitgadget@gmail.com>
Date
Jan 24, 2024, 06:32 UTC
Message-ID
<60ea85a701a05831b0adf1e3f9a7a97fd31ef43f.1706077977.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1646.v2.git.1706077977.gitgitgadget@gmail.com>
From: Mohit Marathe <mohitmarathe23@gmail.com>

This function is an updated version of strtol_i() function. It will give more control to handle parsing of the characters after the integer and better error handling while parsing numbers.

Signed-off-by: Mohit Marathe <mohitmarathe@proton.me>
---
 git-compat-util.h | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
diff --git a/git-compat-util.h b/git-compat-util.h
index 7c2a6538e5a..b38d7c7f8f1 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -1309,6 +1309,29 @@ static inline int strtol_i(char const *s, int base, int *result)
 	return 0;
 }
 
+#define strtol_i(s,b,r) strtol_i_updated((s), (b), (r), NULL)
+static inline int strtol_i_updated(char const *s, int base, int *result, char **endp)
+{
+	long ul;
+	char *dummy = NULL;
+
+	if (!endp)
+		endp = &dummy;
+	errno = 0;
+	ul = strtol(s, endp, base);
+	if (errno ||
+	    /*
+	     * if we are told to parse to the end of the string by
+	     * passing NULL to endp, it is an error to have any
+	     * remaining character after the digits.
+	     */
+	   (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
-- 
gitgitgadget
Previous: Mohit Marathe via GitGitGadgetNext: Mohit Marathe via GitGitGadget
Message 7 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.