Re: [PATCH GSoC v14 02/13] git-compat-util: add strtoul_szt() with error handling
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Jun 25, 2026, 21:09 UTC
- Message-ID
- <xmqqjyrme393.fsf@gitster.g>
- In-Reply-To
- <20260625-ps-eric-work-rebase-v14-2-09f7ffe21a53@gmail.com>
Pablo Sabater <pabloosabaterr@gmail.com> writes:
Show 5 quoted lines
> From: Eric Ju <eric.peijian@gmail.com> > > 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.
But this one no longer returns an unsigned long anymore ;-)
> This variant is needed in a subsequent commit to enable returning an > size_t with proper error handling.
I think it would allow a lot of code paths that want to deal with size_t not to worry about "is ulong large enough?" to have a function like this, but for that to happen, the implementation of the function must carefully think through if these steps do sensible things on platforms with too small ulong (which often is OK when we are coming from decimal string to ulong and then to size_t) and too large ulong (which is not OK, when coming from decimal string to ulong which might be fine, but will bust the size of the final type), etc.
Also, would it make sense to add yet another "static inline" like this? After the dust settles, we may want to rethink these strtoX wrappers we have, benchmark, and possibly make them into a proper library function, not "static inline" that may bloat the runtime.
Show 31 quoted lines
> 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;
> + return 0;
> +}
> +
> static inline int strtol_i(char const *s, int base, int *result)
> {
> long ul;