From 1366244ab6d0c83bf0f90270e5bb608651a3cc8f Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Tue, 15 Mar 2022 15:41:55 +0100 Subject: 9pfs: Use g_new() & friends where that makes obvious sense MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit g_new(T, n) is neater than g_malloc(sizeof(T) * n). It's also safer, for two reasons. One, it catches multiplication overflowing size_t. Two, it returns T * rather than void *, which lets the compiler catch more type errors. This commit only touches allocations with size arguments of the form sizeof(T). Initial patch created mechanically with: $ spatch --in-place --sp-file scripts/coccinelle/use-g_new-etc.cocci \ --macro-file scripts/cocci-macro-file.h FILES... This uncovers a typing error: ../hw/9pfs/9p.c: In function ‘qid_path_fullmap’: ../hw/9pfs/9p.c:855:13: error: assignment to ‘QpfEntry *’ from incompatible pointer type ‘QppEntry *’ [-Werror=incompatible-pointer-types] 855 | val = g_new0(QppEntry, 1); | ^ Harmless, because QppEntry is larger than QpfEntry. Manually fixed to allocate a QpfEntry instead. Cc: Greg Kurz Cc: Christian Schoenebeck Signed-off-by: Markus Armbruster Reviewed-by: Philippe Mathieu-Daudé Reviewed-by: Christian Schoenebeck Reviewed-by: Alex Bennée Reviewed-by: Greg Kurz Message-Id: <20220315144156.1595462-3-armbru@redhat.com> --- hw/9pfs/9p.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'hw/9pfs/9p.c') diff --git a/hw/9pfs/9p.c b/hw/9pfs/9p.c index a6d6b3f..8e9d4ae 100644 --- a/hw/9pfs/9p.c +++ b/hw/9pfs/9p.c @@ -324,7 +324,7 @@ static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid) return NULL; } } - f = g_malloc0(sizeof(V9fsFidState)); + f = g_new0(V9fsFidState, 1); f->fid = fid; f->fid_type = P9_FID_NONE; f->ref = 1; @@ -804,7 +804,7 @@ static int qid_inode_prefix_hash_bits(V9fsPDU *pdu, dev_t dev) val = qht_lookup(&pdu->s->qpd_table, &lookup, hash); if (!val) { - val = g_malloc0(sizeof(QpdEntry)); + val = g_new0(QpdEntry, 1); *val = lookup; affix = affixForIndex(pdu->s->qp_affix_next); val->prefix_bits = affix.bits; @@ -852,7 +852,7 @@ static int qid_path_fullmap(V9fsPDU *pdu, const struct stat *stbuf, return -ENFILE; } - val = g_malloc0(sizeof(QppEntry)); + val = g_new0(QpfEntry, 1); *val = lookup; /* new unique inode and device combo */ @@ -928,7 +928,7 @@ static int qid_path_suffixmap(V9fsPDU *pdu, const struct stat *stbuf, return -ENFILE; } - val = g_malloc0(sizeof(QppEntry)); + val = g_new0(QppEntry, 1); *val = lookup; /* new unique inode affix and device combo */ -- cgit v1.1