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.

Signed-off-by: Roger Pau Monné <roger....@citrix.com>
---
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(-)

diff --git a/tools/firmware/hvmloader/util.c b/tools/firmware/hvmloader/util.c
index 31b4411db7b4..e65134268189 100644
--- a/tools/firmware/hvmloader/util.c
+++ b/tools/firmware/hvmloader/util.c
@@ -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;
     }
 
-- 
2.49.0


Reply via email to