From 02f1503f9fef638f2ed941482b6625a7fc25207c Mon Sep 17 00:00:00 2001 From: wangjianyu3 Date: Tue, 26 Dec 2023 11:21:43 +0800 Subject: [PATCH] libc/stdlib: If there were no digits at all, strtoul() stores the original value of nptr in *endptr (and returns 0). Reference: STRTOUL(3) Signed-off-by: wangjianyu3 --- libs/libc/stdlib/lib_strtoul.c | 6 +++--- libs/libc/string/lib_isbasedigit.c | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/libs/libc/stdlib/lib_strtoul.c b/libs/libc/stdlib/lib_strtoul.c index 3f4959aab7..cc7025afd6 100644 --- a/libs/libc/stdlib/lib_strtoul.c +++ b/libs/libc/stdlib/lib_strtoul.c @@ -52,9 +52,10 @@ unsigned long strtoul(FAR const char *nptr, FAR char **endptr, int base) { + FAR const char *origin = nptr; unsigned long accum = 0; unsigned long limit; - int value; + int value = -1; int last_digit; char sign = 0; @@ -79,7 +80,6 @@ unsigned long strtoul(FAR const char *nptr, FAR char **endptr, int base) if (base < 0) { set_errno(EINVAL); - accum = 0; } else { @@ -127,7 +127,7 @@ unsigned long strtoul(FAR const char *nptr, FAR char **endptr, int base) } } - *endptr = (FAR char *)nptr; + *endptr = (FAR char *)(value == -1 ? origin : nptr); } return accum; diff --git a/libs/libc/string/lib_isbasedigit.c b/libs/libc/string/lib_isbasedigit.c index 2a2d6dd3c6..c9d5b772ae 100644 --- a/libs/libc/string/lib_isbasedigit.c +++ b/libs/libc/string/lib_isbasedigit.c @@ -80,7 +80,7 @@ bool lib_isbasedigit(int ch, int base, int *value) } } - if (value) + if (ret && value) { *value = tmp; }