[PATCH v3] lib: Improve _parse_integer_fixup_radix base 16 detection

Michal Simek monstr at monstr.eu
Mon Apr 6 13:06:52 CEST 2020


pá 20. 3. 2020 v 8:49 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 code detects the first char which is not between 'a'/'A' or 'f'/'F'
> to change base from dec to hex.
>
> Signed-off-by: Michal Simek <michal.simek at xilinx.com>
> Signed-off-by: Shiril Tichkule <shirilt at xlinx.com>
> ---
>
> Changes in v3:
> - Fix commit message to be accurate based on code.
> - Use tolower instead of two ranges
> - Use do while loop - all reported by Andy
> - Remove Tom's reviewed-by tag because code has changed
>
> Changes in v2:
> - Fix end of string to be \0 instead of \n
> - Detect hex by checking chars between a-f to avoid cases like number
>   follow by ;
>
>  lib/strto.c | 15 ++++++++++++++-
>  1 file changed, 14 insertions(+), 1 deletion(-)
>
> diff --git a/lib/strto.c b/lib/strto.c
> index 55ff9f7437d5..1ac2b09c725c 100644
> --- a/lib/strto.c
> +++ b/lib/strto.c
> @@ -22,9 +22,22 @@ static const char *_parse_integer_fixup_radix(const char *s, unsigned int *base)
>                                 *base = 16;
>                         else
>                                 *base = 8;
> -               } else
> +               } else {
> +                       int i = 0;
> +                       char var;
> +
>                         *base = 10;
> +
> +                       do {
> +                               var = tolower(s[i++]);
> +                               if (var >= 'a' && var <= 'f') {
> +                                       *base = 16;
> +                                       break;
> +                               }
> +                       } while (var);
> +               }
>         }
> +
>         if (*base == 16 && s[0] == '0' && tolower(s[1]) == 'x')
>                 s += 2;
>         return s;
> --
> 2.25.1
>

Applied.
M

-- 
Michal Simek, Ing. (M.Eng), OpenPGP -> KeyID: FE3D1F91
w: www.monstr.eu p: +42-0-721842854
Maintainer of Linux kernel - Xilinx Microblaze
Maintainer of Linux kernel - Xilinx Zynq ARM and ZynqMP ARM64 SoCs
U-Boot custodian - Xilinx Microblaze/Zynq/ZynqMP/Versal SoCs


More information about the U-Boot mailing list