qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Philippe Mathieu-Daudé" <philmd@linaro.org>
To: Peter Maydell <peter.maydell@linaro.org>, qemu-devel@nongnu.org
Cc: "Michael S. Tsirkin" <mst@redhat.com>,
	Marcel Apfelbaum <marcel.apfelbaum@gmail.com>,
	Jonathan Cameron <Jonathan.Cameron@huawei.com>
Subject: Re: [PATCH for-8.1] hw/pci-bridge/cxl_upstream.c: Use g_new0() in build_cdat_table()
Date: Tue, 18 Jul 2023 14:10:24 +0200	[thread overview]
Message-ID: <961f67fe-8c79-d1e3-a58c-5516f00bb249@linaro.org> (raw)
In-Reply-To: <20230718101327.1111374-1-peter.maydell@linaro.org>

On 18/7/23 12:13, Peter Maydell wrote:
> In build_cdat_table() we do:
>   *cdat_table = g_malloc0(sizeof(*cdat_table) * CXL_USP_CDAT_NUM_ENTRIES);
> This is wrong because:
>   - cdat_table has type CDATSubHeader ***

Yes

>   - so *cdat_table has type CDATSubHeader **

Yes

>   - so the array we're allocating here should be items of type CDATSubHeader *

Yes

>   - but we pass sizeof(*cdat_table), which is sizeof(CDATSubHeader **),

Indeed

>     implying that we're allocating an array of CDATSubHeader **

Ouch

> It happens that sizeof(CDATSubHeader **) == sizeof(CDATSubHeader *)

Ah!

> so nothing blows up, but this should be sizeof(**cdat_table).

Still, what a mess :)

> Avoid this excessively hard-to-understand code by using
> g_new0() instead, which will do the type checking for us.
> While we're here, we can drop the useless check against failure,
> as g_malloc0() and g_new0() never fail.

Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>

> This fixes Coverity issue CID 1508120.
> 
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> ---
> Disclaimer: I have not tested this beyond any testing you
> get from 'make check' and 'make check-avocado'.
> ---
>   hw/pci-bridge/cxl_upstream.c | 5 +----
>   1 file changed, 1 insertion(+), 4 deletions(-)



  reply	other threads:[~2023-07-18 12:11 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-18 10:13 [PATCH for-8.1] hw/pci-bridge/cxl_upstream.c: Use g_new0() in build_cdat_table() Peter Maydell
2023-07-18 12:10 ` Philippe Mathieu-Daudé [this message]
2023-07-18 12:59   ` Jonathan Cameron via

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=961f67fe-8c79-d1e3-a58c-5516f00bb249@linaro.org \
    --to=philmd@linaro.org \
    --cc=Jonathan.Cameron@huawei.com \
    --cc=marcel.apfelbaum@gmail.com \
    --cc=mst@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).