diff mbox

[for-2.2,1/3] block/raw-posix: Fix preallocating write() loop

Message ID 1416306186-19053-2-git-send-email-mreitz@redhat.com
State New
Headers show

Commit Message

Max Reitz Nov. 18, 2014, 10:23 a.m. UTC
write() may write less bytes than requested; in this case, the number of
bytes written is returned. This is the byte count we should be
subtracting from the number of bytes still to be written, and not the
byte count we requested to write.

Reported-by: László Érsek <lersek@redhat.com>
Signed-off-by: Max Reitz <mreitz@redhat.com>
---
An interesting anecdote: My German man page for write(2) says the
following about its return value:

> Bei Erfolg wird Null zurückgegeben.

Which translates to:

> On success, zero is returned.

Whereas write(2) for LANG=C says the following (which is correct):

> On success, the number of bytes written is returned (zero indicates
> nothing was written).

I think I know why I somehow prefer the English versions...
---
 block/raw-posix.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
diff mbox

Patch

diff --git a/block/raw-posix.c b/block/raw-posix.c
index e100ae2..4e6552f 100644
--- a/block/raw-posix.c
+++ b/block/raw-posix.c
@@ -1457,7 +1457,7 @@  static int raw_create(const char *filename, QemuOpts *opts, Error **errp)
                                  "Could not write to the new file");
                 break;
             }
-            left -= num;
+            left -= result;
         }
         fsync(fd);
         g_free(buf);