From 23344846f6dc501288453908233a4e4682458d17 Mon Sep 17 00:00:00 2001 From: lilei19 Date: Fri, 10 Feb 2023 17:39:13 +0800 Subject: [PATCH] fix some bug of strtold with the test of sim:lua ,I have fixed some bug of error output 1.input: " " and error output string is "NULL" 2.input: "1.2e+", error output string is "NULL" 3.input: "." error output string is "NULL" --- libs/libc/stdlib/lib_strtold.c | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/libs/libc/stdlib/lib_strtold.c b/libs/libc/stdlib/lib_strtold.c index 5338b7c183..2bc5b9002f 100644 --- a/libs/libc/stdlib/lib_strtold.c +++ b/libs/libc/stdlib/lib_strtold.c @@ -119,7 +119,7 @@ static long_long scanexp(FAR char **f, bool flag) c = shgetc(s); - if (c == '+' || c == '-') + if ((c == '+' || c == '-') && isdigit(*s)) { neg = (c == '-'); c = shgetc(s); @@ -335,13 +335,6 @@ static long_double decfloat(FAR char *ptr, FAR char **endptr) } } - if (num_digit == 0) - { - shunget(f); - ifexist(endptr, f); - return zero; - } - if ((c | 32) == 'e') { num_decimal = scanexp(&f, 1) + num_decimal; @@ -357,6 +350,11 @@ static long_double decfloat(FAR char *ptr, FAR char **endptr) } ifexist(endptr, f); + if (num_digit == 0) + { + return zero; + } + f = ptr; k = 0; @@ -475,7 +473,7 @@ static long_double hexfloat(FAR char *ptr, } } - for (; c - '0' < 10 || (c | 32) - 'a' < 6 || c == '.'; c = shgetc(f)) + for (; isxdigit(c) || c == '.'; c = shgetc(f)) { if (c == '.') { @@ -519,7 +517,6 @@ static long_double hexfloat(FAR char *ptr, if (!gotdig) { - shunget(f); shunget(f); if (gotrad) { @@ -642,7 +639,7 @@ static long_double hexfloat(FAR char *ptr, static long_double strtox(FAR const char *str, FAR char **endptr, int flag) { FAR char *s = (FAR char *)str; - int negative = 0; + bool negative = 0; long_double y = 0; int i = 0; @@ -662,6 +659,7 @@ static long_double strtox(FAR const char *str, FAR char **endptr, int flag) case 3: bits = LDBL_MANT_DIG, emin = LDBL_MIN_EXP - bits; + break; default: return 0; } @@ -718,10 +716,15 @@ static long_double strtox(FAR const char *str, FAR char **endptr, int flag) s += 2; y = hexfloat(s, endptr, bits, emin); } - else + else if (isdigit(*s) || (*s == '.' && isdigit(*(s + 1)))) { y = decfloat(s, endptr); } + else + { + ifexist(endptr, (FAR char *)str); + return 0; + } return negative ? -y : y; }