mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-08 14:05:19 +00:00
conf: tighten up XML integer parsing
https://bugzilla.redhat.com/show_bug.cgi?id=617711 reported that even with my recent patched to allow <memory unit='G'>1</memory>, people can still get away with trying <memory>1G</memory> and silently get <memory unit='KiB'>1</memory> instead. While virt-xml-validate catches the error, our C parser did not. Not to mention that it's always fun to fix bugs while reducing lines of code. :) * src/conf/domain_conf.c (virDomainParseMemory): Check for parse error. (virDomainDefParseXML): Avoid strtoll. * src/conf/storage_conf.c (virStorageDefParsePerms): Likewise. * src/util/xml.c (virXPathLongBase, virXPathULongBase) (virXPathULongLong, virXPathLongLong): Likewise.
This commit is contained in:
parent
834bb44834
commit
8a55d381ae
@ -7609,10 +7609,16 @@ virDomainParseMemory(const char *xpath, xmlXPathContextPtr ctxt,
|
|||||||
virReportOOMError();
|
virReportOOMError();
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
if (virXPathULongLong(xpath_full, ctxt, &bytes) < 0) {
|
ret = virXPathULongLong(xpath_full, ctxt, &bytes);
|
||||||
if (required)
|
if (ret < 0) {
|
||||||
|
if (ret == -2)
|
||||||
virDomainReportError(VIR_ERR_XML_ERROR,
|
virDomainReportError(VIR_ERR_XML_ERROR,
|
||||||
"%s", _("missing memory element"));
|
_("could not parse memory element %s"),
|
||||||
|
xpath);
|
||||||
|
else if (required)
|
||||||
|
virDomainReportError(VIR_ERR_XML_ERROR,
|
||||||
|
_("missing memory element %s"),
|
||||||
|
xpath);
|
||||||
else
|
else
|
||||||
ret = 0;
|
ret = 0;
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
@ -8086,12 +8092,11 @@ static virDomainDefPtr virDomainDefParseXML(virCapsPtr caps,
|
|||||||
if (STREQ(tmp, "reset")) {
|
if (STREQ(tmp, "reset")) {
|
||||||
def->clock.data.utc_reset = true;
|
def->clock.data.utc_reset = true;
|
||||||
} else {
|
} else {
|
||||||
char *conv = NULL;
|
if (virStrToLong_ll(tmp, NULL, 10,
|
||||||
unsigned long long val;
|
&def->clock.data.variable.adjustment) < 0) {
|
||||||
val = strtoll(tmp, &conv, 10);
|
virDomainReportError(VIR_ERR_XML_ERROR,
|
||||||
if (conv == tmp || *conv != '\0') {
|
_("unknown clock adjustment '%s'"),
|
||||||
virDomainReportError(VIR_ERR_INTERNAL_ERROR,
|
tmp);
|
||||||
_("unknown clock adjustment '%s'"), tmp);
|
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
switch (def->clock.offset) {
|
switch (def->clock.offset) {
|
||||||
@ -8103,7 +8108,6 @@ static virDomainDefPtr virDomainDefParseXML(virCapsPtr caps,
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
def->clock.offset = VIR_DOMAIN_CLOCK_OFFSET_VARIABLE;
|
def->clock.offset = VIR_DOMAIN_CLOCK_OFFSET_VARIABLE;
|
||||||
def->clock.data.variable.adjustment = val;
|
|
||||||
}
|
}
|
||||||
VIR_FREE(tmp);
|
VIR_FREE(tmp);
|
||||||
} else {
|
} else {
|
||||||
|
@ -570,14 +570,15 @@ virStorageDefParsePerms(xmlXPathContextPtr ctxt,
|
|||||||
if (!mode) {
|
if (!mode) {
|
||||||
perms->mode = defaultmode;
|
perms->mode = defaultmode;
|
||||||
} else {
|
} else {
|
||||||
char *end = NULL;
|
int tmp;
|
||||||
perms->mode = strtol(mode, &end, 8);
|
|
||||||
if (*end || (perms->mode & ~0777)) {
|
if (virStrToLong_i(mode, NULL, 8, &tmp) < 0 || (tmp & ~0777)) {
|
||||||
VIR_FREE(mode);
|
VIR_FREE(mode);
|
||||||
virStorageReportError(VIR_ERR_XML_ERROR,
|
virStorageReportError(VIR_ERR_XML_ERROR,
|
||||||
"%s", _("malformed octal mode"));
|
"%s", _("malformed octal mode"));
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
perms->mode = tmp;
|
||||||
VIR_FREE(mode);
|
VIR_FREE(mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -174,15 +174,8 @@ virXPathLongBase(const char *xpath,
|
|||||||
ctxt->node = relnode;
|
ctxt->node = relnode;
|
||||||
if ((obj != NULL) && (obj->type == XPATH_STRING) &&
|
if ((obj != NULL) && (obj->type == XPATH_STRING) &&
|
||||||
(obj->stringval != NULL) && (obj->stringval[0] != 0)) {
|
(obj->stringval != NULL) && (obj->stringval[0] != 0)) {
|
||||||
char *conv = NULL;
|
if (virStrToLong_l((char *) obj->stringval, NULL, base, value) < 0)
|
||||||
long val;
|
|
||||||
|
|
||||||
val = strtol((const char *) obj->stringval, &conv, base);
|
|
||||||
if (conv == (const char *) obj->stringval) {
|
|
||||||
ret = -2;
|
ret = -2;
|
||||||
} else {
|
|
||||||
*value = val;
|
|
||||||
}
|
|
||||||
} else if ((obj != NULL) && (obj->type == XPATH_NUMBER) &&
|
} else if ((obj != NULL) && (obj->type == XPATH_NUMBER) &&
|
||||||
(!(isnan(obj->floatval)))) {
|
(!(isnan(obj->floatval)))) {
|
||||||
*value = (long) obj->floatval;
|
*value = (long) obj->floatval;
|
||||||
@ -287,15 +280,8 @@ virXPathULongBase(const char *xpath,
|
|||||||
ctxt->node = relnode;
|
ctxt->node = relnode;
|
||||||
if ((obj != NULL) && (obj->type == XPATH_STRING) &&
|
if ((obj != NULL) && (obj->type == XPATH_STRING) &&
|
||||||
(obj->stringval != NULL) && (obj->stringval[0] != 0)) {
|
(obj->stringval != NULL) && (obj->stringval[0] != 0)) {
|
||||||
char *conv = NULL;
|
if (virStrToLong_ul((char *) obj->stringval, NULL, base, value) < 0)
|
||||||
long val;
|
|
||||||
|
|
||||||
val = strtoul((const char *) obj->stringval, &conv, base);
|
|
||||||
if (conv == (const char *) obj->stringval) {
|
|
||||||
ret = -2;
|
ret = -2;
|
||||||
} else {
|
|
||||||
*value = val;
|
|
||||||
}
|
|
||||||
} else if ((obj != NULL) && (obj->type == XPATH_NUMBER) &&
|
} else if ((obj != NULL) && (obj->type == XPATH_NUMBER) &&
|
||||||
(!(isnan(obj->floatval)))) {
|
(!(isnan(obj->floatval)))) {
|
||||||
*value = (unsigned long) obj->floatval;
|
*value = (unsigned long) obj->floatval;
|
||||||
@ -411,15 +397,8 @@ virXPathULongLong(const char *xpath,
|
|||||||
ctxt->node = relnode;
|
ctxt->node = relnode;
|
||||||
if ((obj != NULL) && (obj->type == XPATH_STRING) &&
|
if ((obj != NULL) && (obj->type == XPATH_STRING) &&
|
||||||
(obj->stringval != NULL) && (obj->stringval[0] != 0)) {
|
(obj->stringval != NULL) && (obj->stringval[0] != 0)) {
|
||||||
char *conv = NULL;
|
if (virStrToLong_ull((char *) obj->stringval, NULL, 10, value) < 0)
|
||||||
unsigned long long val;
|
|
||||||
|
|
||||||
val = strtoull((const char *) obj->stringval, &conv, 10);
|
|
||||||
if (conv == (const char *) obj->stringval) {
|
|
||||||
ret = -2;
|
ret = -2;
|
||||||
} else {
|
|
||||||
*value = val;
|
|
||||||
}
|
|
||||||
} else if ((obj != NULL) && (obj->type == XPATH_NUMBER) &&
|
} else if ((obj != NULL) && (obj->type == XPATH_NUMBER) &&
|
||||||
(!(isnan(obj->floatval)))) {
|
(!(isnan(obj->floatval)))) {
|
||||||
*value = (unsigned long long) obj->floatval;
|
*value = (unsigned long long) obj->floatval;
|
||||||
@ -465,15 +444,8 @@ virXPathLongLong(const char *xpath,
|
|||||||
ctxt->node = relnode;
|
ctxt->node = relnode;
|
||||||
if ((obj != NULL) && (obj->type == XPATH_STRING) &&
|
if ((obj != NULL) && (obj->type == XPATH_STRING) &&
|
||||||
(obj->stringval != NULL) && (obj->stringval[0] != 0)) {
|
(obj->stringval != NULL) && (obj->stringval[0] != 0)) {
|
||||||
char *conv = NULL;
|
if (virStrToLong_ll((char *) obj->stringval, NULL, 10, value) < 0)
|
||||||
unsigned long long val;
|
|
||||||
|
|
||||||
val = strtoll((const char *) obj->stringval, &conv, 10);
|
|
||||||
if (conv == (const char *) obj->stringval) {
|
|
||||||
ret = -2;
|
ret = -2;
|
||||||
} else {
|
|
||||||
*value = val;
|
|
||||||
}
|
|
||||||
} else if ((obj != NULL) && (obj->type == XPATH_NUMBER) &&
|
} else if ((obj != NULL) && (obj->type == XPATH_NUMBER) &&
|
||||||
(!(isnan(obj->floatval)))) {
|
(!(isnan(obj->floatval)))) {
|
||||||
*value = (long long) obj->floatval;
|
*value = (long long) obj->floatval;
|
||||||
|
Loading…
Reference in New Issue
Block a user