[Spice-devel] [cacard 4/7] simpletlv: Use g_malloc instead of malloc
Marc-André Lureau
marcandre.lureau at gmail.com
Fri Aug 10 09:54:47 UTC 2018
On Fri, Aug 10, 2018 at 10:04 AM, Christophe Fergeau
<cfergeau at redhat.com> wrote:
> This is more consistent with the rest of the codebase.
>
> Signed-off-by: Christophe Fergeau <cfergeau at redhat.com>
> ---
> src/simpletlv.c | 25 +++++++------------------
> 1 file changed, 7 insertions(+), 18 deletions(-)
>
> diff --git a/src/simpletlv.c b/src/simpletlv.c
> index 42ff572..17ab90e 100644
> --- a/src/simpletlv.c
> +++ b/src/simpletlv.c
> @@ -25,6 +25,7 @@
> #include "config.h"
> #endif
>
> +#include <glib.h>
> #include <stdio.h>
> #include <string.h>
> #include <ctype.h>
> @@ -86,10 +87,7 @@ simpletlv_encode_internal(struct simpletlv_member *tlv, size_t tlv_len,
>
> if (outlen == 0) {
> /* allocate a new buffer */
> - a = malloc(expect_len);
> - if (a == NULL) {
> - return -1;
> - }
> + a = g_malloc(expect_len);
> tmp = a;
> tmp_len = expect_len;
> } else if ((int)outlen >= expect_len) {
> @@ -250,11 +248,9 @@ simpletlv_merge(const struct simpletlv_member *a, size_t a_len,
> struct simpletlv_member *r;
> size_t r_len = a_len + b_len;
>
> - r = malloc(r_len * sizeof(struct simpletlv_member));
> - if (r == NULL)
> - return NULL;
> + r = g_malloc_n(r_len, sizeof(struct simpletlv_member));
Why not g_new?
>
> - /* the uggly way */
> + /* the ugly way */
> offset = a_len * sizeof(struct simpletlv_member);
> memcpy(r, a, offset);
> memcpy(&r[a_len], b, b_len * sizeof(struct simpletlv_member));
> @@ -284,9 +280,7 @@ simpletlv_clone(struct simpletlv_member *tlv, size_t tlvlen)
> size_t i = 0, j;
> struct simpletlv_member *new = NULL;
>
> - new = malloc(sizeof(struct simpletlv_member)*tlvlen);
> - if (!new)
> - goto failure;
> + new = g_malloc_n(tlvlen, sizeof(struct simpletlv_member));
same
>
> for (i = 0; i < tlvlen; i++) {
> new[i].type = tlv[i].type;
> @@ -298,10 +292,7 @@ simpletlv_clone(struct simpletlv_member *tlv, size_t tlvlen)
> if (new[i].value.child == NULL)
> goto failure;
> } else {
> - new[i].value.value = malloc(
> - sizeof(unsigned char)*tlv[i].length);
> - if (new[i].value.value == NULL)
> - goto failure;
> + new[i].value.value = g_malloc_n(tlv[i].length, sizeof(unsigned char));
> memcpy(new[i].value.value, tlv[i].value.value,
> tlv[i].length);
> }
> @@ -354,9 +345,7 @@ simpletlv_parse(unsigned char *data, size_t data_len, size_t *outtlv_len)
>
> tlvp->tag = tag;
> tlvp->length = vlen;
> - tlvp->value.value = malloc(vlen);
> - if (tlvp->value.value == NULL) /* this is fatal */
> - goto failure;
> + tlvp->value.value = g_malloc(vlen);
> memcpy(tlvp->value.value, p, vlen);
> tlvp->type = SIMPLETLV_TYPE_LEAF;
>
> --
> 2.17.1
>
> _______________________________________________
> Spice-devel mailing list
> Spice-devel at lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/spice-devel
--
Marc-André Lureau
More information about the Spice-devel
mailing list