[libvirt] [PATCH v2 1/7] internal: introduce macro helpers to reject exclusive flags

Jeff Nelson jenelson at redhat.com
Fri Mar 27 16:16:32 UTC 2015


On Fri, Mar 27, 2015 at 11:01:22AM +0100, Pavel Hrdina wrote:
> Inspired by commit 7e437ee7 that introduced similar macros for virsh
> commands so we don't have to repeat the same code all over.
> 
> Signed-off-by: Pavel Hrdina <phrdina at redhat.com>
> ---
>  src/internal.h | 44 ++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 44 insertions(+)
> 
> diff --git a/src/internal.h b/src/internal.h
> index 4d473af..eb8d231 100644
> --- a/src/internal.h
> +++ b/src/internal.h
> @@ -327,6 +327,50 @@
>          }                                                               \
>      } while (0)
>  
> +/* Macros to help dealing with mutually exclusive flags. */
> +
> +/**
> + * VIR_EXCLUSIVE_FLAGS_RET:
> + *
> + * @FLAG1: First flag to be checked.
> + * @FLAG2: Second flag to be checked.
> + * @RET: Return value.
> + *
> + * Reject mutually exclusive API flags.  The checked flags are compared
> + * with flags variable.
> + *
> + * This helper does an early return and therefore it has to be called
> + * before anything that would require cleanup.
> + */
> +# define VIR_EXCLUSIVE_FLAGS_RET(FLAG1, FLAG2, RET)                         \
> +    if ((flags & FLAG1) && (flags & FLAG2)) {                               \
> +        virReportInvalidArg(ctl,                                            \
> +                            _("Flags '%s' and '%s' are mutually exclusive"),\
> +                            #FLAG1, #FLAG2);                                \
> +        return RET;                                                         \
> +    }

Please encapsulate within a "do { ... } while(0)" statement. You've
left an -if- statement hidden within the macro definition; the next
-else- to come along will be matched to it:

    if (fred)
      VIR_EXCLUSIVE_FLAGS_GOTO(a,b,c)
    else // matches -if- *inside* macro
      // doesn't execute when you think it does


> +
> +/**
> + * VIR_EXCLUSIVE_FLAGS_GOTO:
> + *
> + * @FLAG1: First flag to be checked.
> + * @FLAG2: Second flag to be checked.
> + * @LABEL: Label to jump to.
> + *
> + * Reject mutually exclusive API flags.  The checked flags are compared
> + * with flags variable.
> + *
> + * Returns nothing.  Jumps to a label if unsupported flags were
> + * passed to it.
> + */
> +# define VIR_EXCLUSIVE_FLAGS_GOTO(FLAG1, FLAG2, LABEL)                      \
> +    if ((flags & FLAG1) && (flags & FLAG2)) {                               \
> +        virReportInvalidArg(ctl,                                            \
> +                            _("Flags '%s' and '%s' are mutually exclusive"),\
> +                            #FLAG1, #FLAG2);                                \
> +        goto LABEL;                                                         \
> +    }
> +

Same feedback as above. Note the macro definition below does this properly.

-Jeff

>  # define virCheckNonNullArgReturn(argname, retval)  \
>      do {                                            \
>          if (argname == NULL) {                      \
> -- 
> 2.0.5
> 
> --
> libvir-list mailing list
> libvir-list at redhat.com
> https://www.redhat.com/mailman/listinfo/libvir-list




More information about the libvir-list mailing list