[PATCH] Return EINVAL for illegal base in strtol

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[PATCH] Return EINVAL for illegal base in strtol

Keith Packard
Signed-off-by: Keith Packard <[hidden email]>
---
 newlib/libc/stdlib/strtol.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/newlib/libc/stdlib/strtol.c b/newlib/libc/stdlib/strtol.c
index f7572b169..897890fe0 100644
--- a/newlib/libc/stdlib/strtol.c
+++ b/newlib/libc/stdlib/strtol.c
@@ -140,6 +140,11 @@ _strtol_l (struct _reent *rptr, const char *__restrict nptr,
  register unsigned long cutoff;
  register int neg = 0, any, cutlim;
 
+ if (base < 0 || base == 1 || base > 36) {
+ errno = EINVAL;
+ return 0;
+ }
+
  /*
  * Skip white space and pick up leading +/- sign if any.
  * If base is 0, allow 0x for hex and 0 for octal, else
@@ -193,9 +198,9 @@ _strtol_l (struct _reent *rptr, const char *__restrict nptr,
  break;
  if (c >= base)
  break;
-               if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
+ if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) {
  any = -1;
- else {
+ } else {
  any = 1;
  acc *= base;
  acc += c;
--
2.24.0

Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] Return EINVAL for illegal base in strtol

Corinna Vinschen
On Dec 16 13:55, Keith Packard wrote:

> Signed-off-by: Keith Packard <[hidden email]>
> ---
>  newlib/libc/stdlib/strtol.c | 9 +++++++--
>  1 file changed, 7 insertions(+), 2 deletions(-)
>
> diff --git a/newlib/libc/stdlib/strtol.c b/newlib/libc/stdlib/strtol.c
> index f7572b169..897890fe0 100644
> --- a/newlib/libc/stdlib/strtol.c
> +++ b/newlib/libc/stdlib/strtol.c
> @@ -140,6 +140,11 @@ _strtol_l (struct _reent *rptr, const char *__restrict nptr,
>   register unsigned long cutoff;
>   register int neg = 0, any, cutlim;
>  
> + if (base < 0 || base == 1 || base > 36) {
> + errno = EINVAL;
> + return 0;
> + }
> +
>   /*
>   * Skip white space and pick up leading +/- sign if any.
>   * If base is 0, allow 0x for hex and 0 for octal, else
> @@ -193,9 +198,9 @@ _strtol_l (struct _reent *rptr, const char *__restrict nptr,
>   break;
>   if (c >= base)
>   break;
> -               if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
> + if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) {
>   any = -1;
> - else {
> + } else {
>   any = 1;
>   acc *= base;
>   acc += c;
> --
> 2.24.0
Pushed.


Thanks,
Corinna

--
Corinna Vinschen
Cygwin Maintainer
Red Hat

signature.asc (849 bytes) Download Attachment