All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Mohit Marathe via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Mohit Marathe <mohitmarathe@proton.me>,
	Mohit Marathe <mohitmarathe23@gmail.com>
Subject: [PATCH v4 1/2] git-compat-util: add strtol_i_updated()
Date: Wed, 24 Jan 2024 06:55:03 +0000	[thread overview]
Message-ID: <60ea85a701a05831b0adf1e3f9a7a97fd31ef43f.1706079304.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.1646.v4.git.1706079304.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


  reply	other threads:[~2024-01-24  6:55 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-22  8:51 [PATCH 0/2] Replace atoi() with strtol_i2() Mohit Marathe via GitGitGadget
2024-01-22  8:51 ` [PATCH 1/2] git-compat-util: add strtol_i2 Mohit Marathe via GitGitGadget
2024-01-22  8:51 ` [PATCH 2/2] patch-id: replace `atoi()` with `strtol_i2()` Mohit Marathe via GitGitGadget
2024-01-22 19:32   ` Junio C Hamano
2024-01-24  4:22     ` Mohit Marathe
2024-01-24  6:32 ` [PATCH v2 0/2] Replace atoi() with strtol_i_updated() Mohit Marathe via GitGitGadget
2024-01-24  6:32   ` [PATCH v2 1/2] git-compat-util: add strtol_i_updated() Mohit Marathe via GitGitGadget
2024-01-24  6:32   ` [PATCH v2 2/2] patch-id: replace `atoi()` with `strtol_i_updated()` Mohit Marathe via GitGitGadget
2024-01-24  6:48   ` [PATCH v3 0/2] Replace atoi() with strtol_i_updated() Mohit Marathe via GitGitGadget
2024-01-24  6:48     ` [PATCH v3 1/2] git-compat-util: add strtol_i_updated() Mohit Marathe via GitGitGadget
2024-01-24  6:48     ` [PATCH v3 2/2] patch-id: replace `atoi()` with `strtol_i_updated()` Mohit Marathe via GitGitGadget
2024-01-24  6:55     ` [PATCH v4 0/2] Replace atoi() with strtol_i_updated() Mohit Marathe via GitGitGadget
2024-01-24  6:55       ` Mohit Marathe via GitGitGadget [this message]
2024-01-24 20:20         ` [PATCH v4 1/2] git-compat-util: add strtol_i_updated() Junio C Hamano
2024-01-24  6:55       ` [PATCH v4 2/2] patch-id: replace `atoi()` with `strtol_i_updated()` Mohit Marathe via GitGitGadget
2024-01-24 21:02         ` Junio C Hamano
2024-01-28  4:35           ` Mohit Marathe
2024-01-28  4:42       ` [PATCH v5 0/2] Replace atoi() with strtoi_with_tail() Mohit Marathe via GitGitGadget
2024-01-28  4:42         ` [PATCH v5 1/2] git-compat-util: add strtoi_with_tail() Mohit Marathe via GitGitGadget
2024-01-30  4:40           ` Junio C Hamano
2024-01-28  4:42         ` [PATCH v5 2/2] patch-id: replace `atoi()` with `strtoi_with_tail` Mohit Marathe via GitGitGadget
2024-02-04  5:48         ` [PATCH v6 0/2] Replace atoi() with strtoi_with_tail() Mohit Marathe via GitGitGadget
2024-02-04  5:48           ` [PATCH v6 1/2] git-compat-util: add strtoi_with_tail() Mohit Marathe via GitGitGadget
2024-02-04  5:48           ` [PATCH v6 2/2] patch-id: replace `atoi()` with `strtoi_with_tail` Mohit Marathe via GitGitGadget

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=60ea85a701a05831b0adf1e3f9a7a97fd31ef43f.1706079304.git.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=mohitmarathe23@gmail.com \
    --cc=mohitmarathe@proton.me \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.