From c62125395bbc494799f8994ce6d3cfbf96b30c72 Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Wed, 4 Jun 2014 11:08:08 +0200 Subject: [PATCH] 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. --- tools/virsh-volume.c | 8 +++---- tools/virsh.c | 51 ++++++++++++++++++++++++++++++++++---------- tools/virsh.h | 3 +++ 3 files changed, 47 insertions(+), 15 deletions(-) diff --git a/tools/virsh-volume.c b/tools/virsh-volume.c index f284fa5222..724a86bc66 100644 --- a/tools/virsh-volume.c +++ b/tools/virsh-volume.c @@ -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; } diff --git a/tools/virsh.c b/tools/virsh.c index 0114771787..1bac84281a 100644 --- a/tools/virsh.c +++ b/tools/virsh.c @@ -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: diff --git a/tools/virsh.h b/tools/virsh.h index 4f5c336b4a..7656407c9b 100644 --- a/tools/virsh.h +++ b/tools/virsh.h @@ -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) -- GitLab