qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Daniel P. Berrange" <berrange@redhat.com>
To: Chih-Min Chao <cmchao@gmail.com>
Cc: qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [PATCH] suppress 'warn_unused_result' warning
Date: Mon, 11 May 2009 17:15:15 +0100	[thread overview]
Message-ID: <20090511161515.GA7858@redhat.com> (raw)
In-Reply-To: <ee84806e0905101215gf32927j1b510a0084038e20@mail.gmail.com>

On Mon, May 11, 2009 at 03:15:11AM +0800, Chih-Min Chao wrote:
> The patch add error handling to functions with 'warn_unused_result' return
> value
> such as write, read, ftruncate, and realpath.

> @@ -239,11 +239,18 @@ static int cow_create(const char *filename, int64_t image_sectors,
>      }
>      cow_header.sectorsize = cpu_to_be32(512);
>      cow_header.size = cpu_to_be64(image_sectors * 512);
> -    write(cow_fd, &cow_header, sizeof(cow_header));
> +    if (write(cow_fd, &cow_header, sizeof(cow_header)) == -1)
> +        goto fail;

This isn't correct. You need to check that the write() actually 
wrote the number of bytes you asked it to., eg 

  if (write(cow_fd, &cow_header, sizeof(cow_header))  != sizeof(cow_header)
     goto fail;

would catch a short write, as well as other errors. Of course
you don't neccessarily want to fail on a short write, because
a reception of a signal can trigger a short write that can 
easily be recovered from by simply calling write() against for
the remainder of the data.

>      /* resize to include at least all the bitmap */
> -    ftruncate(cow_fd, sizeof(cow_header) + ((image_sectors + 7) >> 3));
> +    if (ftruncate(cow_fd, sizeof(cow_header) + ((image_sectors + 7) >> 3)) == -1)
> +        goto fail;
> +
>      close(cow_fd);
>      return 0;
> +
> +fail:
> +    close(cow_fd);
> +    return -1;
>  }
>  
>  static void cow_flush(BlockDriverState *bs)
> diff --git a/block-qcow.c b/block-qcow.c
> index fc6b809..16138f3 100644
> --- a/block-qcow.c
> +++ b/block-qcow.c
> @@ -811,17 +811,28 @@ static int qcow_create(const char *filename, int64_t total_size,
>      }
>  
>      /* write all the data */
> -    write(fd, &header, sizeof(header));
> +    if (write(fd, &header, sizeof(header)) == -1) {
> +        goto fail;
> +    }
> +
>      if (backing_file) {
> -        write(fd, backing_file, backing_filename_len);
> +        if (write(fd, backing_file, backing_filename_len) == -1) {
> +            goto fail;
> +        }
>      }
>      lseek(fd, header_size, SEEK_SET);
>      tmp = 0;
>      for(i = 0;i < l1_size; i++) {
> -        write(fd, &tmp, sizeof(tmp));
> +        if (write(fd, &tmp, sizeof(tmp)) == -1) {
> +            goto fail;
> +        }
>      }

Likewise all these are failing to check for a complete write.

If we want to make this robust for EINTR too, then a small wrapper
around raw read/write calls would likely be wanted to deal with
fact an a signal can cause EINTR, *or* a short write

  ssize_t safewrite(int fd, const void *buf, size_t count)
  {
        size_t nwritten = 0;
        while (count > 0) {
                ssize_t r = write(fd, buf, count);

                if (r < 0 && errno == EINTR)
                        continue;
                if (r < 0)
                        return r;
                if (r == 0)
                        return nwritten;
                buf = (const char *)buf + r;
                count -= r;
                nwritten += r;
        }
        return nwritten;
  }


Regards,
Daniel
-- 
|: Red Hat, Engineering, London   -o-   http://people.redhat.com/berrange/ :|
|: http://libvirt.org  -o-  http://virt-manager.org  -o-  http://ovirt.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505  -o-  F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|

      parent reply	other threads:[~2009-05-11 16:15 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-05-10 19:15 [Qemu-devel] [PATCH] suppress 'warn_unused_result' warning Chih-Min Chao
2009-05-10 22:11 ` Paul Brook
2009-05-10 22:15 ` Stuart Brady
2009-05-10 23:19   ` Anthony Liguori
2009-05-11  1:53   ` M. Warner Losh
2009-05-11 15:42     ` Stuart Brady
2009-05-11 16:02       ` Paul Brook
2009-05-11 16:16       ` Anthony Liguori
2009-05-11 16:25         ` Daniel P. Berrange
2009-05-11 16:57           ` Anthony Liguori
2009-05-12 12:19             ` Jamie Lokier
2009-05-11 17:02       ` Jamie Lokier
2009-05-11 16:15 ` Daniel P. Berrange [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=20090511161515.GA7858@redhat.com \
    --to=berrange@redhat.com \
    --cc=cmchao@gmail.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).