x86/hvmloader: adjust strtoll() to parse hex numbers without 0x prefix
PATCH 1/1 x86/hvmloader: adjust strtoll() to parse hex numbers without 0x prefix
The current strtoll() implementation in hvmloader requires hex number to be prefixed with 0x, otherwise strtoll() won't parse them correctly even when calling the function with base == 16.
Fix this by not unconditionally setting the base to 10 when the string is not 0 prefixed, this also allows parsing octal numbers not 0 prefixed. While there also handle '0X' as a valid hex number prefix, together with '0x'.
No functional change intended to the existing call sites.
---
Noticed this oddity while looking at something else - I don't really have a use case for such parsing, but I think it would be better to get this fixed in case it's needed in the future.
--- tools/firmware/hvmloader/util.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)
tools/firmware/hvmloader/util.c +4 -6
@@ -206,20 +206,18 @@ strtoll(const char *s, char **end, int base)if ( *s =='\0' ) goto out; -if ( *s =='0' ) { +if ( (base ==0 || base ==16 ) && *s =='0' ) { s++;if ( *s =='\0' ) goto out; -if ( *s =='x' ) { -if ( base !=0 && base !=16 ) goto out; +if ( *s =='x' || *s =='X' ) { base =16 ; s++; }else { -if ( base !=0 && base !=8 ) goto out; +if ( base !=0 ) goto out; base =8 ; } - }else { -if (base !=0 && base !=10 ) goto out; + }else if ( base ==0 ) { base =10 ; }
Signature · 1 line
2.49.0
Quoted 4 lines · Roger Pau Monne
The current
strtoll()implementation in hvmloader requires hex number to be prefixed with 0x, otherwisestrtoll()won't parse them correctly even when calling the function with base == 16.
Quoted 8 lines
Fix this by not unconditionally setting the base to 10 when the string is not 0 prefixed, this also allows parsing octal numbers not 0 prefixed. While there also handle '0X' as a valid hex number prefix, together with '0x'.
No functional change intended to the existing call sites.
Signed-off-by Roger Pau Monné <[email protected]>
Quoted 4 lines
---
Noticed this oddity while looking at something else - I don't really have a use case for such parsing, but I think it would be better to get this fixed in case it's needed in the future.
I agree. Without it actually mattering right now, I don't think I'll pick it up for backporting, though.
Jan