Thread (235 messages) 235 messages, 12 authors, 56m ago

Re: [PATCH GSoC v14 02/13] git-compat-util: add strtoul_szt() with error handling

From: Karthik Nayak <hidden>
Date: 2026-06-26 16:43:50

Pablo Sabater [off-list ref] writes:
From: Eric Ju <redacted>

We already have strtoul_ui() and similar functions that provide proper
error handling using strtoul from the standard library. However,
there isn't currently a variant that returns an unsigned long.
Shouldn't this say returns a 'size_t'?
quoted hunk ↗ jump to hunk
This variant is needed in a subsequent commit to enable returning an
size_t with proper error handling.

Signed-off-by: Pablo Sabater <redacted>
---
 git-compat-util.h | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
diff --git a/git-compat-util.h b/git-compat-util.h
index 8809776407..7f417f1acf 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -975,6 +975,26 @@ static inline int strtoul_ui(char const *s, int base, unsigned int *result)
 	return 0;
 }

+/*
+ * Convert a string to a size_t using the standard library's strtoul, with
+ * additional error handling to ensure robustness.
+ */
+static inline int strtoul_szt(char const *s, int base, size_t *result)
+{
+	unsigned long ul;
+	char *p;
+
+	errno = 0;
+	/* negative values would be accepted by strtoul */
+	if (strchr(s, '-'))
+		return -1;
+	ul = strtoul(s, &p, base);
+	if (errno || *p || p == s)
+		return -1;
+	*result = ul;
This converts unsigned long to size_t, but that is fine.

Looks good.
+	return 0;
+}
+
 static inline int strtol_i(char const *s, int base, int *result)
 {
 	long ul;

--
2.54.0

Attachments

Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help