Re: [cacard] simpletlv: Use GArray to make simpletlv_parse simpler

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, 2018-08-13 at 11:08 +0200, Christophe Fergeau wrote:
> This allows to get rid of manual reallocations.

Thank you. This looks more elegant way. Assuming it still works and
passes the CI, it looks good to me.

Acked-by: Jakub Jelen <jjelen@xxxxxxxxxx>

> Signed-off-by: Christophe Fergeau <cfergeau@xxxxxxxxxx>
> ---
>  src/simpletlv.c | 37 +++++++++++--------------------------
>  1 file changed, 11 insertions(+), 26 deletions(-)
> 
> diff --git a/src/simpletlv.c b/src/simpletlv.c
> index 6759ea7..0adba5a 100644
> --- a/src/simpletlv.c
> +++ b/src/simpletlv.c
> @@ -316,12 +316,14 @@ simpletlv_parse(unsigned char *data, size_t
> data_len, size_t *outtlv_len)
>  {
>      unsigned char *p, *p_end;
>      unsigned char tag;
> -    size_t vlen, tlv_len = 0, tlv_allocated = 0;
> -    struct simpletlv_member *tlv = NULL, *tlvp = NULL;
> +    size_t vlen;
> +    GArray *tlv = g_array_new(FALSE, FALSE, sizeof(struct
> simpletlv_member));
>  
>      p = data;
>      p_end = p + data_len;
>      while (p < p_end) {
> +        struct simpletlv_member tlvp;
> +
>          /* we can return what was parsed successfully */
>          if (simpletlv_read_tag(&p, p_end - p, &tag, &vlen) < 0) {
>              break;
> @@ -330,34 +332,17 @@ simpletlv_parse(unsigned char *data, size_t
> data_len, size_t *outtlv_len)
>              break;
>          }
>  
> -        /* Extend the allocated structure if needed */
> -        if (tlv_len+1 > tlv_allocated) {
> -            struct simpletlv_member *newtlv;
> -            tlv_allocated += 10;
> -            newtlv = realloc(tlv, tlv_allocated * sizeof(struct
> simpletlv_member));
> -            if (newtlv == NULL) /* this is fatal */
> -                goto failure;
> -            tlv = newtlv;
> -        }
> -        tlvp = &(tlv[tlv_len++]);
> -        tlvp->value.value = NULL;
> -
> -
> -        tlvp->tag = tag;
> -        tlvp->length = vlen;
> -        tlvp->value.value = g_malloc(vlen);
> -        memcpy(tlvp->value.value, p, vlen);
> -        tlvp->type = SIMPLETLV_TYPE_LEAF;
> +        tlvp.tag = tag;
> +        tlvp.length = vlen;
> +        tlvp.value.value = g_memdup(p, vlen);
> +        tlvp.type = SIMPLETLV_TYPE_LEAF;
> +        g_array_append_val(tlv, tlvp);
>  
>          p += vlen;
>      }
>  
> -    *outtlv_len = tlv_len;
> -    return tlv;
> -
> -failure:
> -    simpletlv_free(tlv, tlv_len);
> -    return NULL;
> +    *outtlv_len = tlv->len;
> +    return (struct simpletlv_member *)g_array_free(tlv, FALSE);
>  }
>  
>  /* vim: set ts=4 sw=4 tw=0 noet expandtab: */
-- 
Jakub Jelen
Software Engineer
Security Technologies
Red Hat, Inc.

_______________________________________________
Spice-devel mailing list
Spice-devel@xxxxxxxxxxxxxxxxxxxxx
https://lists.freedesktop.org/mailman/listinfo/spice-devel




[Index of Archives]     [Linux Virtualization]     [Linux Virtualization]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Security]     [Bugtraq]     [Linux OMAP]     [Linux MIPS]     [ECOS]     [Asterisk Internet PBX]     [Linux API]     [Monitors]