[PATCH] virsh-network: Don't check for virshXXXTypeFromString() >= VIR_XXX_LAST

Michal Privoznik posted 1 patch 2 years, 2 months ago
Test syntax-check failed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/8f4d1d9fcc7c1f46ec7717fe8bb13efef033685e.1641820375.git.mprivozn@redhat.com
tools/virsh-network.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
[PATCH] virsh-network: Don't check for virshXXXTypeFromString() >= VIR_XXX_LAST
Posted by Michal Privoznik 2 years, 2 months ago
They way our VIR_ENUM_IMPL() and virXXXTypeFromString() work is
that for any string that's not recognized a negative one is
returned. And, since VIR_XXX_LAST is passed to VIR_ENUM_IMPL() we
can be sure that all enum members are covered. Therefore, there
is no way that virXXXTypeFromString() can return a value that's
bigger or equal to VIR_XXX_LAST.

I've noticed two places where such comparison was made, both in
cmdNetworkUpdate(). Drop them.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
---
 tools/virsh-network.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/tools/virsh-network.c b/tools/virsh-network.c
index 5f574be78c..b2daf31d6b 100644
--- a/tools/virsh-network.c
+++ b/tools/virsh-network.c
@@ -984,7 +984,7 @@ cmdNetworkUpdate(vshControl *ctl, const vshCmd *cmd)
         command = VIR_NETWORK_UPDATE_COMMAND_ADD_LAST;
     } else {
         command = virshNetworkUpdateCommandTypeFromString(commandStr);
-        if (command <= 0 || command >= VIR_NETWORK_UPDATE_COMMAND_LAST) {
+        if (command <= 0) {
             vshError(ctl, _("unrecognized command name '%s'"), commandStr);
             goto cleanup;
         }
@@ -994,7 +994,7 @@ cmdNetworkUpdate(vshControl *ctl, const vshCmd *cmd)
         goto cleanup;
 
     section = virshNetworkSectionTypeFromString(sectionStr);
-    if (section <= 0 || section >= VIR_NETWORK_SECTION_LAST) {
+    if (section <= 0) {
         vshError(ctl, _("unrecognized section name '%s'"), sectionStr);
         goto cleanup;
     }
-- 
2.34.1

Re: [PATCH] virsh-network: Don't check for virshXXXTypeFromString() >= VIR_XXX_LAST
Posted by Andrea Bolognani 2 years, 2 months ago
On Mon, Jan 10, 2022 at 02:12:55PM +0100, Michal Privoznik wrote:
> They way our VIR_ENUM_IMPL() and virXXXTypeFromString() work is
> that for any string that's not recognized a negative one is
> returned. And, since VIR_XXX_LAST is passed to VIR_ENUM_IMPL() we
> can be sure that all enum members are covered. Therefore, there
> is no way that virXXXTypeFromString() can return a value that's
> bigger or equal to VIR_XXX_LAST.
>
> I've noticed two places where such comparison was made, both in
> cmdNetworkUpdate(). Drop them.
>
> Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
> ---
>  tools/virsh-network.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)

Reviewed-by: Andrea Bolognani <abologna@redhat.com>

-- 
Andrea Bolognani / Red Hat / Virtualization