summaryrefslogtreecommitdiffhomepage
path: root/src/nxt_string.c
diff options
context:
space:
mode:
authorAlejandro Colomar <alx@kernel.org>2023-12-19 13:48:28 +0100
committerAlejandro Colomar <alx@kernel.org>2023-12-19 14:08:14 +0100
commit13dedafc71925d73ef83d0848271bd5e77f6b14b (patch)
tree40fb01541755dcea0767ca8de0e8187a69352e9e /src/nxt_string.c
parent3630425f826c7334592c7d1f2527082cfe43f79d (diff)
downloadunit-13dedafc71925d73ef83d0848271bd5e77f6b14b.tar.gz
unit-13dedafc71925d73ef83d0848271bd5e77f6b14b.tar.bz2
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: <https://blog.habets.se/2022/10/No-way-to-parse-integers-in-C.html> Link: <https://softwareengineering.stackexchange.com/a/449060/332848> Signed-off-by: Alejandro Colomar <alx@kernel.org>
Diffstat (limited to 'src/nxt_string.c')
-rw-r--r--src/nxt_string.c19
1 files changed, 19 insertions, 0 deletions
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 <alx@kernel.org>
* Copyright (C) NGINX, Inc.
*/
#include <nxt_main.h>
+#include <errno.h>
+#include <stdlib.h>
+
+
+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);
+}