Thread (361 messages) 361 messages, 13 authors, 2d ago
WARM2d
Revisions (10)
  1. v1 [diff vs current]
  2. v2 [diff vs current]
  3. v3 [diff vs current]
  4. v4 [diff vs current]
  5. v5 [diff vs current]
  6. v6 [diff vs current]
  7. v7 [diff vs current]
  8. v9 [diff vs current]
  9. v10 current
  10. v11 [diff vs current]

[PATCH v10 1/8] git-compat-util: add strtoul_ul() with error handling

From: Eric Ju <hidden>
Date: 2025-01-14 02:15:27
Subsystem: the rest · Maintainer: Linus Torvalds

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.
This commit introduces strtoul_ul() to address this gap, enabling the
return of an unsigned long with proper error handling.
---
 git-compat-util.h | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
diff --git a/git-compat-util.h b/git-compat-util.h
index e283c46c6f..f2935750bf 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -1351,6 +1351,26 @@ static inline int strtoul_ui(char const *s, int base, unsigned int *result)
 	return 0;
 }
 
+/*
+ * Convert a string to an unsigned long using the standard library's strtoul,
+ * with additional error handling to ensure robustness.
+ */
+static inline int strtoul_ul(char const *s, int base, unsigned long *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;
+	return 0;
+}
+
 static inline int strtol_i(char const *s, int base, int *result)
 {
 	long ul;
-- 
2.47.1
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help