All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jonathan Cameron via <qemu-devel@nongnu.org>
To: "Philippe Mathieu-Daudé" <philmd@linaro.org>
Cc: Hao Zeng <zenghao@kylinos.cn>, <fan.ni@samsung.com>,
	<peter.maydell@linaro.org>, <qemu-devel@nongnu.org>
Subject: Re: [PATCH v2 2/2] cxl-cdat:Fix the check on the return value of fread()
Date: Wed, 12 Apr 2023 14:02:50 +0100	[thread overview]
Message-ID: <20230412140250.00000d4a@Huawei.com> (raw)
In-Reply-To: <07ab950e-7ee4-2476-9032-4638fe6eed1f@linaro.org>

On Wed, 12 Apr 2023 12:02:47 +0200
Philippe Mathieu-Daudé <philmd@linaro.org> wrote:

> On 12/4/23 09:16, Hao Zeng wrote:
> > The bug in this code (CID 1507822) is that the
> > check on the return value of fread() is wrong. fread()
> > returns the number of items read or written, so
> > checking for == 0 only catches "no data read at all",
> > not "only read half the data".
> > 
> > Signed-off-by: Zeng Hao <zenghao@kylinos.cn>
> > Suggested-by: Peter Maydell <peter.maydell@linaro.org>
> > ---
> >   hw/cxl/cxl-cdat.c | 2 +-
> >   1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/hw/cxl/cxl-cdat.c b/hw/cxl/cxl-cdat.c
> > index ba7ed1aafd..130531a9cd 100644
> > --- a/hw/cxl/cxl-cdat.c
> > +++ b/hw/cxl/cxl-cdat.c
> > @@ -126,7 +126,7 @@ static void ct3_load_cdat(CDATObject *cdat, Error **errp)
> >       fseek(fp, 0, SEEK_SET);
> >       cdat->buf = g_malloc0(file_size);  
> 
> Pointless bzero in g_malloc0, however this code would be
> simplified using g_file_get_contents().

Agreed - switching this whole thing to g_file_get_contents()
will get rid of this code and be a lot simpler.
Perhaps just jump directly to that and note the two bugs that existed
in the code that is replaced?

Jonathan
 

> 
> >   
> > -    if (fread(cdat->buf, file_size, 1, fp) == 0) {
> > +    if (fread(cdat->buf, file_size, 1, fp) != file_size) {
> >           error_setg(errp, "CDAT: File read failed");
> >           fclose(fp);
> >           return;  
> 



      reply	other threads:[~2023-04-12 13:05 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-12  7:16 [PATCH v2 0/2] cxl-cdat:Fix two problems of ct3_load_cdat Hao Zeng
2023-04-12  7:16 ` [PATCH v2 1/2] cxl-cdat:Fix open file not closed in ct3_load_cdat Hao Zeng
2023-04-12  9:58   ` Philippe Mathieu-Daudé
2023-04-12  7:16 ` [PATCH v2 2/2] cxl-cdat:Fix the check on the return value of fread() Hao Zeng
2023-04-12 10:02   ` Philippe Mathieu-Daudé
2023-04-12 13:02     ` Jonathan Cameron via [this message]

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=20230412140250.00000d4a@Huawei.com \
    --to=qemu-devel@nongnu.org \
    --cc=Jonathan.Cameron@Huawei.com \
    --cc=fan.ni@samsung.com \
    --cc=peter.maydell@linaro.org \
    --cc=philmd@linaro.org \
    --cc=zenghao@kylinos.cn \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.