Skip to content

Commit 4e26096

Browse files
peijianjugitster
authored andcommitted
git-compat-util: add strtoul_ul() with error handling
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. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2d2a71c commit 4e26096

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

git-compat-util.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,6 +1353,26 @@ static inline int strtoul_ui(char const *s, int base, unsigned int *result)
13531353
return 0;
13541354
}
13551355

1356+
/*
1357+
* Convert a string to an unsigned long using the standard library's strtoul,
1358+
* with additional error handling to ensure robustness.
1359+
*/
1360+
static inline int strtoul_ul(char const *s, int base, unsigned long *result)
1361+
{
1362+
unsigned long ul;
1363+
char *p;
1364+
1365+
errno = 0;
1366+
/* negative values would be accepted by strtoul */
1367+
if (strchr(s, '-'))
1368+
return -1;
1369+
ul = strtoul(s, &p, base);
1370+
if (errno || *p || p == s )
1371+
return -1;
1372+
*result = ul;
1373+
return 0;
1374+
}
1375+
13561376
static inline int strtol_i(char const *s, int base, int *result)
13571377
{
13581378
long ul;

0 commit comments

Comments
 (0)