From 13dedafc71925d73ef83d0848271bd5e77f6b14b Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Tue, 19 Dec 2023 13:48:28 +0100 Subject: String: added nxt_strtoul_noneg(). This function is like strtoul(3), but it rejects negative numbers, instead of silently converting them into unsigned, which strtoul(3) does. Link: Link: Signed-off-by: Alejandro Colomar --- src/nxt_string.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/nxt_string.c b/src/nxt_string.c index 1ca595a1..ed4ff955 100644 --- a/src/nxt_string.c +++ b/src/nxt_string.c @@ -1,11 +1,19 @@ /* * Copyright (C) Igor Sysoev + * Copyright (C) Alejandro Colomar * Copyright (C) NGINX, Inc. */ #include +#include +#include + + +static inline unsigned long nxt_strtoul_noneg(const char *nptr, + char **restrict endptr, int base); + nxt_str_t * nxt_str_alloc(nxt_mp_t *mp, size_t length) @@ -842,3 +850,14 @@ nxt_base64_decode(u_char *dst, u_char *src, size_t length) return (p - dst); } + + +static inline unsigned long +nxt_strtoul_noneg(const char *nptr, char **restrict endptr, int base) +{ + if (strtol(nptr, endptr, base) < 0) { + errno = ERANGE; + return 0; + } + return strtoul(nptr, endptr, base); +} -- cgit