[Spice-devel] [cacard] simpletlv: Use GArray to make simpletlv_parse simpler

Christophe Fergeau cfergeau at redhat.com
Mon Aug 13 15:12:53 UTC 2018


Hey,

On Mon, Aug 13, 2018 at 02:56:43PM +0200, Jakub Jelen wrote:
> 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.

Yup, I've checked CI passes:
https://gitlab.com/teuf/libcacard/commit/d0683ca5c94478ba037ffe238099072bffb88977

Christophe

> 
> Acked-by: Jakub Jelen <jjelen at redhat.com>
> 
> > Signed-off-by: Christophe Fergeau <cfergeau at redhat.com>
> > ---
> >  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.
> 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://lists.freedesktop.org/archives/spice-devel/attachments/20180813/fa5b0abd/attachment.sig>


More information about the Spice-devel mailing list