lib: Improve _parse_integer_fixup_radix base 16 detection
PATCH v2 1/1 lib: Improve _parse_integer_fixup_radix base 16 detection
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.
---
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 | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-)
lib/strto.c +18 -1
@@ -22,9 +22,26 @@ 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 =='\0' ) +break ; + +if ((var >='a' && var <='f' ) || + (var >='A' && var <='F' )) { + *base =16 ; +break ; + } + } + } } +if (*base ==16 && s[0 ] =='0' &&tolower (s[1 ]) =='x' ) s +=2 ;return s;
Signature · 1 line
2.25.1
PATCH v2 1/1 lib: Improve _parse_integer_fixup_radix base 16 detection
Quoted 7 lines · Michal Simek
Base autodetection is failing for this case:
if test 257 -gt 3ae; then echo first; else echo second; fiIt 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.
...
lib/strto.c static const char *_parse_integer_fixup_radix(const char *s, unsigned int *base) · line 41
Quoted 12 lines
lib/strto.c +12
-0+for (i =0 ; ; i++) { +char var = s[i]; + +if (var =='\0' ) +break ; + +if ((var >='a' && var <='f' ) || + (var >='A' && var <='F' )) { + *base =16 ; +break ; + } + }
int i = 0;
char var;
do {
var = tolower(s[i++]);
if (...) {
*base = 16;
break;
}
} while (var);
or alike?
...
Quoted 3 lines
}+
Is this relevant?
Quoted 3 lines
if (*base == 16 && s[0] == '0' && tolower(s[1]) == 'x')
s += 2;
return s;Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH v2 1/1 lib: Improve _parse_integer_fixup_radix base 16 detection
Quoted 29 lines · Andy Shevchenko
Quoted 7 lines · Michal Simek
Base autodetection is failing for this case:
if test 257 -gt 3ae; then echo first; else echo second; fiIt 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....
lib/strto.c static const char *_parse_integer_fixup_radix(const char *s, unsigned int *base) · line 41
Quoted 12 lines
lib/strto.c +12
-0+for (i =0 ; ; i++) { +char var = s[i]; + +if (var =='\0' ) +break ; + +if ((var >='a' && var <='f' ) || + (var >='A' && var <='F' )) { + *base =16 ; +break ; + } + }int i = 0;
char var;do {
var =tolower(s[i++]);
Quoted 7 lines
if (...) { *base = 16; break;}
} while (var);or alike?
not a problem with this.
Quoted 8 lines
...
Quoted 3 lines
}+
Is this relevant?
it can be removed from this patch but I can't see any issue with it to making code more readable.
Quoted 5 lines
Quoted 4 lines
if (*base == 16 && s[0] == '0' && tolower(s[1]) == 'x') s += 2; return s;
M
PATCH v2 1/1 lib: Improve _parse_integer_fixup_radix base 16 detection
Quoted 11 lines · Michal Simek
Quoted 10 lines · Andy Shevchenko
Quoted 8 lines · Michal Simek
Base autodetection is failing for this case:
if test 257 -gt 3ae; then echo first; else echo second; fiIt 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.
Hold on!
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
lib/strto.c +4
-0+for (i =0 ; ; i++) { +char var = s[i]; + +if (var =='\0' )
Quoted 8 lines
Quoted 8 lines
lib/strto.c static const char *_parse_integer_fixup_radix(const char *s, unsigned int *base) · line 41
Quoted 8 lines
lib/strto.c +8
-0+break ; + +if ((var >='a' && var <='f' ) || + (var >='A' && var <='F' )) { + *base =16 ; +break ; + } + }
Actually your code and commit message are unaligned. In the code you stop on the *first* hex character without checking the rest.
Signature · 2 lines
With Best Regards, Andy Shevchenko
PATCH v2 1/1 lib: Improve _parse_integer_fixup_radix base 16 detection
Quoted 15 lines · Andy Shevchenko
Quoted 11 lines · Michal Simek
Quoted 10 lines · Andy Shevchenko
Quoted 8 lines · Michal Simek
Base autodetection is failing for this case:
if test 257 -gt 3ae; then echo first; else echo second; fiIt 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.
Hold on!
Quoted 15 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
lib/strto.c +4
-0+for (i =0 ; ; i++) { +char var = s[i]; + +if (var =='\0' )Quoted 8 lines
Quoted 8 lines
lib/strto.c static const char *_parse_integer_fixup_radix(const char *s, unsigned int *base) · line 41
Quoted 8 lines
lib/strto.c +8
-0+break ; + +if ((var >='a' && var <='f' ) || + (var >='A' && var <='F' )) { + *base =16 ; +break ; + } + }Actually your code and commit message are unaligned. In the code you stop on the *first* hex character without checking the rest.
That's correct because base is detected as 10 at this stage.
The question is if make sense to check all chars because if there is one char between a<= <=f then you are sure that it is not base 10.
And if you want to check all of them then you need to also detect chars like ';' which can be there as you see above.
Thanks,
Michal