libs/libc/stdlib: fix strtoul,strtoull bugs when value outside range

Prototype:
  unsigned long strtoul(FAR const char *nptr, FAR char **endptr, int base);
  unsigned long long strtoull(FAR const char *nptr,  FAR char **endptr, int base);

If endptr is not NULL, strtoul()/strtoull() should store the address of the first
invalid character in *endptr. And if the correct value is outside the range of
representable values, {ULONG_MAX} or {ULLONG_MAX} shall be returned and errno set
to [ERANGE].

With such code:
  strtoul("34592348345343453453455645765736575865767", &endptr, 10);

It indeed returns ULONG_MAX and sets errno to ERANGE. But after strtoul
return, endptr points to "3455645765736575865767", not NULL.

Signed-off-by: Sunny <zxcvbnm37425@gmail.com>
This commit is contained in:
Sunny 2023-07-10 12:56:16 +00:00 committed by Petro Karashchenko
parent 3ef9f4a0b7
commit 2a5f48c3fd
2 changed files with 10 additions and 0 deletions

View file

@ -103,6 +103,11 @@ unsigned long strtoul(FAR const char *nptr, FAR char **endptr, int base)
nptr++;
}
while (lib_isbasedigit(*nptr, base, &value))
{
nptr++;
}
if (sign == '-')
{
accum = (~accum) + 1;

View file

@ -107,6 +107,11 @@ unsigned long long strtoull(FAR const char *nptr,
nptr++;
}
while (lib_isbasedigit(*nptr, base, &value))
{
nptr++;
}
if (sign == '-')
{
accum = (~accum) + 1;