提交 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, ...@@ -1070,7 +1070,7 @@ int virConfGetValueBool(virConfPtr conf,
return -1; return -1;
} }
if (cval->l < 0 || cval->l > 1) { if (((unsigned long long)cval->l) > 1) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("%s: value for '%s' parameter must be 0 or 1"), _("%s: value for '%s' parameter must be 0 or 1"),
conf->filename, setting); conf->filename, setting);
...@@ -1167,7 +1167,7 @@ int virConfGetValueUInt(virConfPtr conf, ...@@ -1167,7 +1167,7 @@ int virConfGetValueUInt(virConfPtr conf,
return -1; return -1;
} }
if (cval->l > UINT_MAX || cval->l < 0) { if (((unsigned long long)cval->l) > UINT_MAX) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("%s: value for '%s' parameter must be in range 0:%u"), _("%s: value for '%s' parameter must be in range 0:%u"),
conf->filename, setting, UINT_MAX); conf->filename, setting, UINT_MAX);
...@@ -1208,14 +1208,13 @@ int virConfGetValueSizeT(virConfPtr conf, ...@@ -1208,14 +1208,13 @@ int virConfGetValueSizeT(virConfPtr conf,
if (!cval) if (!cval)
return 0; return 0;
if (cval->type == VIR_CONF_LONG) { if (cval->type != VIR_CONF_ULONG) {
if (cval->l < 0 || cval->l > SIZE_MAX) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("%s: value for '%s' parameter must be in range 0:%zu"), _("%s: expected an unsigned integer for '%s' parameter"),
conf->filename, setting, SIZE_MAX); conf->filename, setting);
return -1; return -1;
} }
} else if (cval->type == VIR_CONF_ULONG) {
#if ULLONG_MAX > SIZE_MAX #if ULLONG_MAX > SIZE_MAX
if (((unsigned long long)cval->l) > SIZE_MAX) { if (((unsigned long long)cval->l) > SIZE_MAX) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
...@@ -1224,12 +1223,6 @@ int virConfGetValueSizeT(virConfPtr conf, ...@@ -1224,12 +1223,6 @@ int virConfGetValueSizeT(virConfPtr conf,
return -1; return -1;
} }
#endif #endif
} else {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("%s: expected an unsigned integer for '%s' parameter"),
conf->filename, setting);
return -1;
}
*value = (size_t)cval->l; *value = (size_t)cval->l;
...@@ -1369,14 +1362,7 @@ int virConfGetValueULLong(virConfPtr conf, ...@@ -1369,14 +1362,7 @@ int virConfGetValueULLong(virConfPtr conf,
if (!cval) if (!cval)
return 0; return 0;
if (cval->type == VIR_CONF_LONG) { if (cval->type != VIR_CONF_ULONG) {
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) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("%s: expected an unsigned integer for '%s' parameter"), _("%s: expected an unsigned integer for '%s' parameter"),
conf->filename, setting); conf->filename, setting);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册