qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Peter Maydell <peter.maydell@linaro.org>
To: Bandan Das <bsd@redhat.com>
Cc: Gerd Hoffmann <kraxel@redhat.com>,
	QEMU Developers <qemu-devel@nongnu.org>,
	Eduardo Habkost <ehabkost@redhat.com>
Subject: Re: [Qemu-devel] [PULL 7/8] usb-mtp: breakup MTP write into smaller chunks
Date: Fri, 15 Feb 2019 18:55:29 +0000	[thread overview]
Message-ID: <CAFEAcA8Q_73RQZOwqJSVA8AaY2D82n4Tb66gvdWP90mdroJs6Q@mail.gmail.com> (raw)
In-Reply-To: <jpgef886gbk.fsf@linux.bootlegged.copy>

On Fri, 15 Feb 2019 at 18:45, Bandan Das <bsd@redhat.com> wrote:
>
> Peter Maydell <peter.maydell@linaro.org> writes:
> > CID 1398642: This early-return case in usb_mtp_write_data() returns
> > from the function without doing any of the cleanup (closing file,
> > freeing data, etc). Possibly it should be "goto done;" instead ?
> > The specific thing Coverity complains about is the memory pointed
> > to by "path".
> >
>
> I believe this is a false positive, there's still more data incoming
> and we have successfully written the data we got this time, so we return
> without freeing up any of the structures. I will add a comment here.

Looking at the code I think Coverity is correct about the 'path'
string. We allocate this with
            path = g_strdup_printf("%s/%s", parent->path, s->dataset.filename);
and then use it in a mkdir() and an open() call, and never
save the pointer anywhere. But we only g_free() it in the
exit paths that go through "free:".

One simple fix to this would be to narrow the scope of 'path',
so we deal with it only inside the if():

        if (s->dataset.filename) {
            char *path = g_strdup_printf("%s/%s", parent->path,
s->dataset.filename);
            if (s->dataset.format == FMT_ASSOCIATION) {
                d->fd = mkdir(path, mask);
                g_free(path);
                goto free;
            }
            d->fd = open(path, O_CREAT | O_WRONLY |
                         O_CLOEXEC | O_NOFOLLOW, mask);
            g_free(path);
            if (d->fd == -1) {
                usb_mtp_queue_result(s, RES_STORE_FULL, d->trans,
                                     0, 0, 0, 0);
                goto done;
            }
            [...]

and then drop the g_free(path) from the end of the function.


While I'm looking at this, that call to mkdir() looks bogus:
                d->fd = mkdir(path, mask);
mkdir() does not return a file descriptor, it returns a
0-or-negative status code (which we are not checking),
so storing its result into d->fd looks very weird. If
we ever do try to treat d->fd as an fd later on then we
will end up operating on stdin, which is going to have
very confusing effects.


If the MTPState* is kept around and reused, should the
cleanup code that does close(d->fd) also set d->fd to -1,
so that we know that the fd has been closed and don't
later try to close it twice or otherwise use it ?

thanks
-- PMM

  reply	other threads:[~2019-02-15 18:55 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-30  7:34 [Qemu-devel] [PULL 0/8] Usb 20190130 patches Gerd Hoffmann
2019-01-30  7:34 ` [Qemu-devel] [PULL 1/8] usb: assign unique serial numbers to hid devices Gerd Hoffmann
2019-05-17 16:54   ` Dr. David Alan Gilbert
2019-01-30  7:34 ` [Qemu-devel] [PULL 2/8] usb: dev-mtp: close fd in usb_mtp_object_readdir() Gerd Hoffmann
2019-01-30  7:34 ` [Qemu-devel] [PULL 3/8] hw/usb: Fix LGPL information in the file headers Gerd Hoffmann
2019-01-30  7:34 ` [Qemu-devel] [PULL 4/8] usb: XHCI shall not halt isochronous endpoints Gerd Hoffmann
2019-01-30  7:34 ` [Qemu-devel] [PULL 5/8] usb: implement XHCI underrun/overrun events Gerd Hoffmann
2019-01-30  7:34 ` [Qemu-devel] [PULL 6/8] usb-mtp: Reallocate buffer in multiples of MTP_WRITE_BUF_SZ Gerd Hoffmann
2019-01-30  7:34 ` [Qemu-devel] [PULL 7/8] usb-mtp: breakup MTP write into smaller chunks Gerd Hoffmann
2019-02-14 18:52   ` Peter Maydell
2019-02-15 18:45     ` Bandan Das
2019-02-15 18:55       ` Peter Maydell [this message]
2019-02-15 19:22         ` Bandan Das
2019-01-30  7:34 ` [Qemu-devel] [PULL 8/8] usb-mtp: replace the homebrew write with qemu_write_full Gerd Hoffmann
2019-01-31 15:40 ` [Qemu-devel] [PULL 0/8] Usb 20190130 patches Peter Maydell
2019-01-31 18:10 ` no-reply
2019-02-02 21:26 ` no-reply

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=CAFEAcA8Q_73RQZOwqJSVA8AaY2D82n4Tb66gvdWP90mdroJs6Q@mail.gmail.com \
    --to=peter.maydell@linaro.org \
    --cc=bsd@redhat.com \
    --cc=ehabkost@redhat.com \
    --cc=kraxel@redhat.com \
    --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).