[PATCH] libxl: simplify and correct domain ID and CPU pool ID parsing

Jan Beulich posted 1 patch 3 weeks, 6 days ago
[PATCH] libxl: simplify and correct domain ID and CPU pool ID parsing
Posted by Jan Beulich 3 weeks, 6 days ago
strtoul(), with a radix of 10, already guarantees that the string parsed
consists of just digits (and leading whitespace). No need to do that check
in advance. All which needs doing is a check for a leading digit, to
exclude the case of leading whitespace.

This in particular avoids a domain ID argument of "" to be mis-treated as
a reference to Dom0.

While similar mis-treatment isn't present there, do the same
simplification also in the adjacent qualifier_to_id().

Fixes: ef496b81f033 ("libxl: postpone backend name resolution")
Reported-by: Msd <msd-xenproject.org@msd.im>
Signed-off-by: Jan Beulich <jbeulich@suse.com>
---
This retains prior behavior as to leading (and trailing) whitespace (none
permitted). Whether that's intended behavior I don't know.

The "end != ..." checks are likely paranoia: If the first character is a
digit as per isdigit(), strtoul() should have consumed it.

Likely there are more similar issues elsewhere, as many invocations of
strto*() pass NULL as endptr argument.

--- a/tools/libs/light/libxl_utils.c
+++ b/tools/libs/light/libxl_utils.c
@@ -91,16 +91,16 @@ int libxl_name_to_domid(libxl_ctx *ctx,
 int libxl_domain_qualifier_to_domid(libxl_ctx *ctx, const char *name,
                                     uint32_t *domid)
 {
-    int i, rv;
-    for (i=0; name[i]; i++) {
-        if (!CTYPE(isdigit, name[i])) {
-            goto nondigit_found;
-        }
+    int rv;
+
+    if (CTYPE(isdigit, *name)) {
+        char *end;
+
+        *domid = strtoul(name, &end, 10);
+        if (end != name && !*end)
+            return 0;
     }
-    *domid = strtoul(name, NULL, 10);
-    return 0;
 
- nondigit_found:
     /* this could also check for uuids */
     rv = libxl_name_to_domid(ctx, name, domid);
     return rv;
@@ -108,22 +108,15 @@ int libxl_domain_qualifier_to_domid(libx
 
 static int qualifier_to_id(const char *p, uint32_t *id_r)
 {
-    int i, alldigit;
+    if (CTYPE(isdigit, *p)) {
+        char *end;
 
-    alldigit = 1;
-    for (i = 0; p[i]; i++) {
-        if (!isdigit((uint8_t)p[i])) {
-            alldigit = 0;
-            break;
-        }
+        *id_r = strtoul(p, &end, 10);
+        if (end != p && !*end)
+            return 0;
     }
 
-    if (i > 0 && alldigit) {
-        *id_r = strtoul(p, NULL, 10);
-        return 0;
-    } else {
-        /* check here if it's a uuid and do proper conversion */
-    }
+    /* check here if it's a uuid and do proper conversion */
     return 1;
 }
Re: [PATCH] libxl: simplify and correct domain ID and CPU pool ID parsing
Posted by Jürgen Groß 3 weeks, 6 days ago
On 27.10.26 17:00, Jan Beulich wrote:
> strtoul(), with a radix of 10, already guarantees that the string parsed
> consists of just digits (and leading whitespace). No need to do that check
> in advance. All which needs doing is a check for a leading digit, to
> exclude the case of leading whitespace.
> 
> This in particular avoids a domain ID argument of "" to be mis-treated as
> a reference to Dom0.
> 
> While similar mis-treatment isn't present there, do the same
> simplification also in the adjacent qualifier_to_id().
> 
> Fixes: ef496b81f033 ("libxl: postpone backend name resolution")
> Reported-by: Msd <msd-xenproject.org@msd.im>
> Signed-off-by: Jan Beulich <jbeulich@suse.com>
> ---
> This retains prior behavior as to leading (and trailing) whitespace (none
> permitted). Whether that's intended behavior I don't know.
> 
> The "end != ..." checks are likely paranoia: If the first character is a
> digit as per isdigit(), strtoul() should have consumed it.

I'd drop the test, as it really is redundant.

With or without dropping it:

Reviewed-by: Juergen Gross <jgross@suse.com>


Juergen