Convert calls to g_malloc() and g_malloc0() to g_new() and g_new0()
respectively, in cases where the return value is casted to the same
type as specified using sizeof(type) as passed to g_malloc().

Coccinelle did not match these when matching assignments involving an
expression corresponding to the type used for determining the size to
allocate.  Such cases deserve more careful review, in case the types do
not match, so are submitted separately.

This was achieved using Coccinelle with the following semantic patch:

@@ type T; @@
-(T *)g_malloc(sizeof(T))
+g_new(T, 1)

@@ type T; @@
-(T *)g_malloc0(sizeof(T))
+g_new0(T, 1)

@@ type T; expression N; @@
-(T *)g_malloc(sizeof(T) * N)
+g_new(T, N)

@@ type T; expression N; @@
-(T *)g_malloc0(sizeof(T) * N)
+g_new0(T, N)

Signed-off-by: Stuart Brady <s...@zubnet.me.uk>
---
 hw/sd.c                    |    2 +-
 libcacard/vcard_emul_nss.c |    2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/hw/sd.c b/hw/sd.c
index c6186c1..038d60c 100644
--- a/hw/sd.c
+++ b/hw/sd.c
@@ -410,7 +410,7 @@ static void sd_reset(SDState *sd, BlockDriverState *bdrv)
     if (sd->wp_groups)
         g_free(sd->wp_groups);
     sd->wp_switch = bdrv ? bdrv_is_read_only(bdrv) : 0;
-    sd->wp_groups = (int *) g_malloc0(sizeof(int) * sect);
+    sd->wp_groups = g_new0(int, sect);
     memset(sd->function_group, 0, sizeof(int) * 6);
     sd->erase_start = 0;
     sd->erase_end = 0;
diff --git a/libcacard/vcard_emul_nss.c b/libcacard/vcard_emul_nss.c
index 8897bae..795aeaa 100644
--- a/libcacard/vcard_emul_nss.c
+++ b/libcacard/vcard_emul_nss.c
@@ -1180,7 +1180,7 @@ vcard_emul_options(const char *args)
                 g_strndup(type_params, type_params_length);
             count = count_tokens(args, ',', ')') + 1;
             vreaderOpt->cert_count = count;
-            vreaderOpt->cert_name = (char **)g_malloc(count*sizeof(char *));
+            vreaderOpt->cert_name = g_new(char *, count);
             for (i = 0; i < count; i++) {
                 const char *cert = args;
                 args = strpbrk(args, ",)");
-- 
1.7.4.1


Reply via email to