[libvirt] [PATCH] virsh: check if specified debug level is in range
--- tools/virsh.c | 4 ++++ 1 files changed, 4 insertions(+), 0 deletions(-) diff --git a/tools/virsh.c b/tools/virsh.c index 5658796..34ae171 100644 --- a/tools/virsh.c +++ b/tools/virsh.c @@ -3097,6 +3097,10 @@ vshParseArgv(vshControl *ctl, int argc, char **argv) vshError(ctl, "%s", _("option -d takes a numeric argument")); exit(EXIT_FAILURE); } + if (ctl->debug < VSH_ERR_DEBUG || ctl->debug > VSH_ERR_ERROR) { + vshError(ctl, _("ignoring debug level %d out of range [0-4]"), + ctl->debug); + } break; case 'h': vshUsage(); -- 1.7.8.6
On 07/26/2012 07:35 AM, Ján Tomko wrote:
--- tools/virsh.c | 4 ++++ 1 files changed, 4 insertions(+), 0 deletions(-)
diff --git a/tools/virsh.c b/tools/virsh.c index 5658796..34ae171 100644 --- a/tools/virsh.c +++ b/tools/virsh.c @@ -3097,6 +3097,10 @@ vshParseArgv(vshControl *ctl, int argc, char **argv) vshError(ctl, "%s", _("option -d takes a numeric argument")); exit(EXIT_FAILURE); } + if (ctl->debug < VSH_ERR_DEBUG || ctl->debug > VSH_ERR_ERROR) { + vshError(ctl, _("ignoring debug level %d out of range [0-4]"), + ctl->debug); + }
That warns, but still uses the fishy ctl->debug value for the rest of the life of the program. Would it be better to first parse the option into a temporary variable, then range check, and only after the range check then assign it to ctl->debug? -- Eric Blake eblake@redhat.com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
--- Forgot exit in v1. V2 doesn't exit if the value is numeric, it just ignores it if it's out of range. --- tools/virsh.c | 9 +++++++-- 1 files changed, 7 insertions(+), 2 deletions(-) diff --git a/tools/virsh.c b/tools/virsh.c index 5658796..4f8ea94 100644 --- a/tools/virsh.c +++ b/tools/virsh.c @@ -3073,7 +3073,7 @@ vshAllowedEscapeChar(char c) static bool vshParseArgv(vshControl *ctl, int argc, char **argv) { - int arg, len; + int arg, len, debug; struct option opt[] = { {"debug", required_argument, NULL, 'd'}, {"help", no_argument, NULL, 'h'}, @@ -3093,10 +3093,15 @@ vshParseArgv(vshControl *ctl, int argc, char **argv) while ((arg = getopt_long(argc, argv, "+d:hqtc:vVrl:e:", opt, NULL)) != -1) { switch (arg) { case 'd': - if (virStrToLong_i(optarg, NULL, 10, &ctl->debug) < 0) { + if (virStrToLong_i(optarg, NULL, 10, &debug) < 0) { vshError(ctl, "%s", _("option -d takes a numeric argument")); exit(EXIT_FAILURE); } + if (debug < VSH_ERR_DEBUG || debug > VSH_ERR_ERROR) + vshError(ctl, _("ignoring debug level %d out of range [%d-%d]"), + debug, VSH_ERR_DEBUG, VSH_ERR_ERROR); + else + ctl->debug = debug; break; case 'h': vshUsage(); -- 1.7.8.6
On 07/26/2012 08:05 AM, Ján Tomko wrote:
--- Forgot exit in v1. V2 doesn't exit if the value is numeric, it just ignores it if it's out of range. --- tools/virsh.c | 9 +++++++-- 1 files changed, 7 insertions(+), 2 deletions(-)
ACK and pushed. -- Eric Blake eblake@redhat.com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
participants (2)
-
Eric Blake -
Ján Tomko