Skip to content

Commit 2334484

Browse files
lilei19xiaoxiang781216
authored andcommitted
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"
1 parent 807d596 commit 2334484

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

libs/libc/stdlib/lib_strtold.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ static long_long scanexp(FAR char **f, bool flag)
119119

120120
c = shgetc(s);
121121

122-
if (c == '+' || c == '-')
122+
if ((c == '+' || c == '-') && isdigit(*s))
123123
{
124124
neg = (c == '-');
125125
c = shgetc(s);
@@ -335,13 +335,6 @@ static long_double decfloat(FAR char *ptr, FAR char **endptr)
335335
}
336336
}
337337

338-
if (num_digit == 0)
339-
{
340-
shunget(f);
341-
ifexist(endptr, f);
342-
return zero;
343-
}
344-
345338
if ((c | 32) == 'e')
346339
{
347340
num_decimal = scanexp(&f, 1) + num_decimal;
@@ -357,6 +350,11 @@ static long_double decfloat(FAR char *ptr, FAR char **endptr)
357350
}
358351

359352
ifexist(endptr, f);
353+
if (num_digit == 0)
354+
{
355+
return zero;
356+
}
357+
360358
f = ptr;
361359

362360
k = 0;
@@ -475,7 +473,7 @@ static long_double hexfloat(FAR char *ptr,
475473
}
476474
}
477475

478-
for (; c - '0' < 10 || (c | 32) - 'a' < 6 || c == '.'; c = shgetc(f))
476+
for (; isxdigit(c) || c == '.'; c = shgetc(f))
479477
{
480478
if (c == '.')
481479
{
@@ -519,7 +517,6 @@ static long_double hexfloat(FAR char *ptr,
519517

520518
if (!gotdig)
521519
{
522-
shunget(f);
523520
shunget(f);
524521
if (gotrad)
525522
{
@@ -642,7 +639,7 @@ static long_double hexfloat(FAR char *ptr,
642639
static long_double strtox(FAR const char *str, FAR char **endptr, int flag)
643640
{
644641
FAR char *s = (FAR char *)str;
645-
int negative = 0;
642+
bool negative = 0;
646643
long_double y = 0;
647644
int i = 0;
648645

@@ -662,6 +659,7 @@ static long_double strtox(FAR const char *str, FAR char **endptr, int flag)
662659
case 3:
663660
bits = LDBL_MANT_DIG,
664661
emin = LDBL_MIN_EXP - bits;
662+
break;
665663
default:
666664
return 0;
667665
}
@@ -718,10 +716,15 @@ static long_double strtox(FAR const char *str, FAR char **endptr, int flag)
718716
s += 2;
719717
y = hexfloat(s, endptr, bits, emin);
720718
}
721-
else
719+
else if (isdigit(*s) || (*s == '.' && isdigit(*(s + 1))))
722720
{
723721
y = decfloat(s, endptr);
724722
}
723+
else
724+
{
725+
ifexist(endptr, (FAR char *)str);
726+
return 0;
727+
}
725728

726729
return negative ? -y : y;
727730
}

0 commit comments

Comments
 (0)