提交 3742cec8 编写于 作者: A Andrea Bolognani

util: conf: Improve virConfGet*() logic

When parsing numeric values, we always store them as unsigned
unless they're negative. We can use this fact to simplify the
logic by removing a bunch of unnecessary checks.
上级 ac3ba191
......@@ -1070,7 +1070,7 @@ int virConfGetValueBool(virConfPtr conf,
return -1;
}
if (cval->l < 0 || cval->l > 1) {
if (((unsigned long long)cval->l) > 1) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("%s: value for '%s' parameter must be 0 or 1"),
conf->filename, setting);
......@@ -1167,7 +1167,7 @@ int virConfGetValueUInt(virConfPtr conf,
return -1;
}
if (cval->l > UINT_MAX || cval->l < 0) {
if (((unsigned long long)cval->l) > UINT_MAX) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("%s: value for '%s' parameter must be in range 0:%u"),
conf->filename, setting, UINT_MAX);
......@@ -1208,29 +1208,22 @@ int virConfGetValueSizeT(virConfPtr conf,
if (!cval)
return 0;
if (cval->type == VIR_CONF_LONG) {
if (cval->l < 0 || cval->l > SIZE_MAX) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("%s: value for '%s' parameter must be in range 0:%zu"),
conf->filename, setting, SIZE_MAX);
return -1;
}
} else if (cval->type == VIR_CONF_ULONG) {
#if ULLONG_MAX > SIZE_MAX
if (((unsigned long long)cval->l) > SIZE_MAX) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("%s: value for '%s' parameter must be in range 0:%zu"),
conf->filename, setting, SIZE_MAX);
return -1;
}
#endif
} else {
if (cval->type != VIR_CONF_ULONG) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("%s: expected an unsigned integer for '%s' parameter"),
conf->filename, setting);
return -1;
}
#if ULLONG_MAX > SIZE_MAX
if (((unsigned long long)cval->l) > SIZE_MAX) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("%s: value for '%s' parameter must be in range 0:%zu"),
conf->filename, setting, SIZE_MAX);
return -1;
}
#endif
*value = (size_t)cval->l;
return 1;
......@@ -1369,14 +1362,7 @@ int virConfGetValueULLong(virConfPtr conf,
if (!cval)
return 0;
if (cval->type == VIR_CONF_LONG) {
if (cval->l < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("%s: value for '%s' parameter must be in range 0:%llu"),
conf->filename, setting, ULLONG_MAX);
return -1;
}
} else if (cval->type != VIR_CONF_ULONG) {
if (cval->type != VIR_CONF_ULONG) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("%s: expected an unsigned integer for '%s' parameter"),
conf->filename, setting);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册