libxl: simplify and correct domain ID and CPU pool ID parsing
PATCH 1/1 libxl: simplify and correct domain ID and CPU pool ID parsing
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().
---
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.
tools/libs/light/libxl_utils.c +14 -21
@@ -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(libxstatic 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 ; }
Quoted 14 lines · Jan Beulich
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 <[email protected]>Signed-off-by Jan Beulich <[email protected]>
Quoted 6 lines
---
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:
Juergen