提交 c6212539 编写于 作者: P Peter Krempa

virsh: Reject negative numbers in vshCommandOptULongLong

To follow the new semantics of the vshCommandOptToU* functions convert
this one to reject negative numbers too. To allow using -1 for "maximum"
semantics for the vol-*load two bandwidth functions that use this helper
introduce vshCommandOptULongLongWrap.
上级 0e2d7305
......@@ -677,12 +677,12 @@ cmdVolUpload(vshControl *ctl, const vshCmd *cmd)
const char *name = NULL;
unsigned long long offset = 0, length = 0;
if (vshCommandOptULongLong(cmd, "offset", &offset) < 0) {
if (vshCommandOptULongLongWrap(cmd, "offset", &offset) < 0) {
vshError(ctl, _("Unable to parse integer"));
return false;
}
if (vshCommandOptULongLong(cmd, "length", &length) < 0) {
if (vshCommandOptULongLongWrap(cmd, "length", &length) < 0) {
vshError(ctl, _("Unable to parse integer"));
return false;
}
......@@ -787,12 +787,12 @@ cmdVolDownload(vshControl *ctl, const vshCmd *cmd)
unsigned long long offset = 0, length = 0;
bool created = false;
if (vshCommandOptULongLong(cmd, "offset", &offset) < 0) {
if (vshCommandOptULongLongWrap(cmd, "offset", &offset) < 0) {
vshError(ctl, _("Unable to parse integer"));
return false;
}
if (vshCommandOptULongLong(cmd, "length", &length) < 0) {
if (vshCommandOptULongLongWrap(cmd, "length", &length) < 0) {
vshError(ctl, _("Unable to parse integer"));
return false;
}
......
......@@ -1699,31 +1699,60 @@ vshCommandOptLongLong(const vshCmd *cmd, const char *name,
return 1;
}
static int
vshCommandOptULongLongInternal(const vshCmd *cmd,
const char *name,
unsigned long long *value,
bool wrap)
{
vshCmdOpt *arg;
int ret;
if ((ret = vshCommandOpt(cmd, name, &arg, true)) <= 0)
return ret;
if (wrap) {
if (virStrToLong_ull(arg->data, NULL, 10, value) < 0)
return -1;
} else {
if (virStrToLong_ullp(arg->data, NULL, 10, value) < 0)
return -1;
}
return 1;
}
/**
* vshCommandOptULongLong:
* @cmd command reference
* @name option name
* @value result
*
* Returns option as long long
* Returns option as long long, rejects negative numbers
* See vshCommandOptInt()
*/
int
vshCommandOptULongLong(const vshCmd *cmd, const char *name,
unsigned long long *value)
{
vshCmdOpt *arg;
int ret;
ret = vshCommandOpt(cmd, name, &arg, true);
if (ret <= 0)
return ret;
if (virStrToLong_ull(arg->data, NULL, 10, value) < 0)
return -1;
return 1;
return vshCommandOptULongLongInternal(cmd, name, value, false);
}
/**
* vshCommandOptULongLongWrap:
* @cmd command reference
* @name option name
* @value result
*
* Returns option as long long, wraps negative numbers to positive
* See vshCommandOptInt()
*/
int
vshCommandOptULongLongWrap(const vshCmd *cmd, const char *name,
unsigned long long *value)
{
return vshCommandOptULongLongInternal(cmd, name, value, true);
}
/**
* vshCommandOptScaledInt:
......
......@@ -307,6 +307,9 @@ int vshCommandOptLongLong(const vshCmd *cmd, const char *name,
int vshCommandOptULongLong(const vshCmd *cmd, const char *name,
unsigned long long *value)
ATTRIBUTE_NONNULL(3) ATTRIBUTE_RETURN_CHECK;
int vshCommandOptULongLongWrap(const vshCmd *cmd, const char *name,
unsigned long long *value)
ATTRIBUTE_NONNULL(3) ATTRIBUTE_RETURN_CHECK;
int vshCommandOptScaledInt(const vshCmd *cmd, const char *name,
unsigned long long *value, int scale,
unsigned long long max)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册