diff mbox series

lib: Improve _parse_integer_fixup_radix base 16 detection

Message ID 8588cd961afbc7ef0fde66aad728d85d53d1c5e0.1581077243.git.michal.simek@xilinx.com
State Superseded
Headers show
Series lib: Improve _parse_integer_fixup_radix base 16 detection | expand

Commit Message

Michal Simek Feb. 7, 2020, 12:07 p.m. UTC
Base autodetection is failing for this case:
if test 257 -gt 3ae; then echo first; else echo second; fi

It is because base for 3ae is recognized by _parse_integer_fixup_radix() as
10. The patch is checking all chars to make sure that they are not 'a' or
up. If they are base needs to be in hex.

Signed-off-by: Michal Simek <michal.simek at xilinx.com>
---

 lib/strto.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

Comments

Michal Simek Feb. 13, 2020, 7:33 a.m. UTC | #1
p? 7. 2. 2020 v 13:07 odes?latel Michal Simek <michal.simek at xilinx.com> napsal:
>
> Base autodetection is failing for this case:
> if test 257 -gt 3ae; then echo first; else echo second; fi
>
> It is because base for 3ae is recognized by _parse_integer_fixup_radix() as
> 10. The patch is checking all chars to make sure that they are not 'a' or
> up. If they are base needs to be in hex.
>
> Signed-off-by: Michal Simek <michal.simek at xilinx.com>
> ---
>
>  lib/strto.c | 18 +++++++++++++++++-
>  1 file changed, 17 insertions(+), 1 deletion(-)
>
> diff --git a/lib/strto.c b/lib/strto.c
> index 55ff9f7437d5..14821c87f16d 100644
> --- a/lib/strto.c
> +++ b/lib/strto.c
> @@ -22,9 +22,25 @@ static const char *_parse_integer_fixup_radix(const char *s, unsigned int *base)
>                                 *base = 16;
>                         else
>                                 *base = 8;
> -               } else
> +               } else {
> +                       int i;
> +
>                         *base = 10;
> +
> +                       for (i = 0; ; i++) {
> +                               char var = s[i];
> +
> +                               if (var == '\n')
> +                                       break;
> +
> +                               if (var >= 'a') {
> +                                       *base = 16;
> +                                       break;
> +                               }
> +                       }
> +               }
>         }
> +
>         if (*base == 16 && s[0] == '0' && tolower(s[1]) == 'x')
>                 s += 2;
>         return s;
> --
> 2.25.0
>

Tom: Any comment on this one?

M
Tom Rini Feb. 13, 2020, 12:30 p.m. UTC | #2
On Thu, Feb 13, 2020 at 08:33:09AM +0100, Michal Simek wrote:
> p? 7. 2. 2020 v 13:07 odes?latel Michal Simek <michal.simek at xilinx.com> napsal:
> >
> > Base autodetection is failing for this case:
> > if test 257 -gt 3ae; then echo first; else echo second; fi
> >
> > It is because base for 3ae is recognized by _parse_integer_fixup_radix() as
> > 10. The patch is checking all chars to make sure that they are not 'a' or
> > up. If they are base needs to be in hex.
> >
> > Signed-off-by: Michal Simek <michal.simek at xilinx.com>
> > ---
> >
> >  lib/strto.c | 18 +++++++++++++++++-
> >  1 file changed, 17 insertions(+), 1 deletion(-)
> >
> > diff --git a/lib/strto.c b/lib/strto.c
> > index 55ff9f7437d5..14821c87f16d 100644
> > --- a/lib/strto.c
> > +++ b/lib/strto.c
> > @@ -22,9 +22,25 @@ static const char *_parse_integer_fixup_radix(const char *s, unsigned int *base)
> >                                 *base = 16;
> >                         else
> >                                 *base = 8;
> > -               } else
> > +               } else {
> > +                       int i;
> > +
> >                         *base = 10;
> > +
> > +                       for (i = 0; ; i++) {
> > +                               char var = s[i];
> > +
> > +                               if (var == '\n')
> > +                                       break;
> > +
> > +                               if (var >= 'a') {
> > +                                       *base = 16;
> > +                                       break;
> > +                               }
> > +                       }
> > +               }
> >         }
> > +
> >         if (*base == 16 && s[0] == '0' && tolower(s[1]) == 'x')
> >                 s += 2;
> >         return s;
> > --
> > 2.25.0
> >
> 
> Tom: Any comment on this one?

Reviewed-by: Tom Rini <trini at konsulko.com>
Michal Simek Feb. 20, 2020, 6:46 a.m. UTC | #3
?t 13. 2. 2020 v 13:30 odes?latel Tom Rini <trini at konsulko.com> napsal:
>
> On Thu, Feb 13, 2020 at 08:33:09AM +0100, Michal Simek wrote:
> > p? 7. 2. 2020 v 13:07 odes?latel Michal Simek <michal.simek at xilinx.com> napsal:
> > >
> > > Base autodetection is failing for this case:
> > > if test 257 -gt 3ae; then echo first; else echo second; fi
> > >
> > > It is because base for 3ae is recognized by _parse_integer_fixup_radix() as
> > > 10. The patch is checking all chars to make sure that they are not 'a' or
> > > up. If they are base needs to be in hex.
> > >
> > > Signed-off-by: Michal Simek <michal.simek at xilinx.com>
> > > ---
> > >
> > >  lib/strto.c | 18 +++++++++++++++++-
> > >  1 file changed, 17 insertions(+), 1 deletion(-)
> > >
> > > diff --git a/lib/strto.c b/lib/strto.c
> > > index 55ff9f7437d5..14821c87f16d 100644
> > > --- a/lib/strto.c
> > > +++ b/lib/strto.c
> > > @@ -22,9 +22,25 @@ static const char *_parse_integer_fixup_radix(const char *s, unsigned int *base)
> > >                                 *base = 16;
> > >                         else
> > >                                 *base = 8;
> > > -               } else
> > > +               } else {
> > > +                       int i;
> > > +
> > >                         *base = 10;
> > > +
> > > +                       for (i = 0; ; i++) {
> > > +                               char var = s[i];
> > > +
> > > +                               if (var == '\n')

here should be \0

> > > +                                       break;
> > > +
> > > +                               if (var >= 'a') {

Colleague also suggested to check also till f here.

Will create v2 on this.

M
diff mbox series

Patch

diff --git a/lib/strto.c b/lib/strto.c
index 55ff9f7437d5..14821c87f16d 100644
--- a/lib/strto.c
+++ b/lib/strto.c
@@ -22,9 +22,25 @@  static const char *_parse_integer_fixup_radix(const char *s, unsigned int *base)
 				*base = 16;
 			else
 				*base = 8;
-		} else
+		} else {
+			int i;
+
 			*base = 10;
+
+			for (i = 0; ; i++) {
+				char var = s[i];
+
+				if (var == '\n')
+					break;
+
+				if (var >= 'a') {
+					*base = 16;
+					break;
+				}
+			}
+		}
 	}
+
 	if (*base == 16 && s[0] == '0' && tolower(s[1]) == 'x')
 		s += 2;
 	return s;