Re: [PATCH GSoC RFC v12 02/12] git-compat-util: add strtoul_ul() with error handling
- From
- Chandra Pratap <chandrapratap3519@gmail.com>
- Date
- Jun 9, 2026, 06:20 UTC
- Message-ID
- <CA+J6zkST0Hs-X-2csfnfri4UVdK-yBDdQALkBcRK62a88aZ15Q@mail.gmail.com>
- In-Reply-To
- <20260608-ps-eric-work-rebase-v12-2-5338b766e658@gmail.com>
On Mon, 8 Jun 2026 at 15:44, Pablo Sabater <pabloosabaterr@gmail.com> wrote:
Show 10 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. > > This variant is needed in a subsequent commit. > > Add strtoul_ul() to address this gap, enabling the
Nit: extra space here. Also, this could be conciser. Maybe something like:
"This variant is needed in a subsequent commit to enable returning an unsigned long with proper error handling."
Show 41 quoted lines
> return of an unsigned long with proper error handling.
>
> Signed-off-by: Pablo Sabater <pabloosabaterr@gmail.com>
> ---
> git-compat-util.h | 20 ++++++++++++++++++++
> 1 file changed, 20 insertions(+)
>
> diff --git a/git-compat-util.h b/git-compat-util.h
> index 8809776407..4bf569f35c 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 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.54.0