[libvirt] [PATCH v1 18/32] util: netdevopenvswitch: use VIR_AUTOPTR for aggregate types

Erik Skultety eskultet at redhat.com
Fri Aug 3 13:35:48 UTC 2018


On Sat, Jul 28, 2018 at 11:31:33PM +0530, Sukrit Bhatnagar wrote:
> By making use of GNU C's cleanup attribute handled by the
> VIR_AUTOPTR macro for declaring aggregate pointer variables,
> majority of the calls to *Free functions can be dropped, which
> in turn leads to getting rid of most of our cleanup sections.
>
> Signed-off-by: Sukrit Bhatnagar <skrtbhtngr at gmail.com>
> ---
>  src/util/virnetdevopenvswitch.c | 106 +++++++++++++++-------------------------
>  1 file changed, 40 insertions(+), 66 deletions(-)
>
> diff --git a/src/util/virnetdevopenvswitch.c b/src/util/virnetdevopenvswitch.c
> index a9c5e2a..eae5861 100644
> --- a/src/util/virnetdevopenvswitch.c
> +++ b/src/util/virnetdevopenvswitch.c
> @@ -76,9 +76,7 @@ virNetDevOpenvswitchAddTimeout(virCommandPtr cmd)
>  static int
>  virNetDevOpenvswitchConstructVlans(virCommandPtr cmd, virNetDevVlanPtr virtVlan)
>  {
> -    int ret = -1;
>      size_t i = 0;
> -    virBuffer buf = VIR_BUFFER_INITIALIZER;
>
>      if (!virtVlan || !virtVlan->nTags)
>          return 0;
> @@ -98,7 +96,12 @@ virNetDevOpenvswitchConstructVlans(virCommandPtr cmd, virNetDevVlanPtr virtVlan)
>      }
>
>      if (virtVlan->trunk) {
> -        virBufferAddLit(&buf, "trunk=");
> +        VIR_AUTOPTR(virBuffer) buf = NULL;
> +
> +        if (VIR_ALLOC(buf) < 0)
> +            return -1;
> +
> +        virBufferAddLit(buf, "trunk=");
>
>          /*
>           * Trunk ports have at least one VLAN. Do the first one
> @@ -106,24 +109,21 @@ virNetDevOpenvswitchConstructVlans(virCommandPtr cmd, virNetDevVlanPtr virtVlan)
>           * start of the for loop if there are more than one VLANs
>           * on this trunk port.
>           */
> -        virBufferAsprintf(&buf, "%d", virtVlan->tag[i]);
> +        virBufferAsprintf(buf, "%d", virtVlan->tag[i]);
>
>          for (i = 1; i < virtVlan->nTags; i++) {
> -            virBufferAddLit(&buf, ",");
> -            virBufferAsprintf(&buf, "%d", virtVlan->tag[i]);
> +            virBufferAddLit(buf, ",");
> +            virBufferAsprintf(buf, "%d", virtVlan->tag[i]);
>          }
>
> -        if (virBufferCheckError(&buf) < 0)
> -            goto cleanup;
> -        virCommandAddArg(cmd, virBufferCurrentContent(&buf));
> +        if (virBufferCheckError(buf) < 0)
> +            return -1;
> +        virCommandAddArg(cmd, virBufferCurrentContent(buf));
>      } else if (virtVlan->nTags) {
>          virCommandAddArgFormat(cmd, "tag=%d", virtVlan->tag[0]);
>      }
>
> -    ret = 0;
> - cleanup:
> -    virBufferFreeAndReset(&buf);
> -    return ret;
> +    return 0;

The obvious problem with virBuffer :)...otherwise it's fine, + the ordering
issue of the declarations, that applies to all patches.

Erik




More information about the libvir-list mailing list