Matthew Booth wrote:
On 17/11/09 17:07, Richard W.M. Jones wrote:
From 3c5d63be17d281634f036f53aa32103ad4ab1c41 Mon Sep 17 00:00:00 2001 From: Richard Jones<rjones@redhat.com> Date: Tue, 17 Nov 2009 17:02:45 +0000 Subject: [PATCH 4/5] fuse: Fix read for empty files.
Error handling for the guestfs_pread call was incorrect, which meant that empty files could produce spurious error messages. --- fuse/guestmount.c | 8 +++++++- 1 files changed, 7 insertions(+), 1 deletions(-)
diff --git a/fuse/guestmount.c b/fuse/guestmount.c index 9d16cef..669bf80 100644 --- a/fuse/guestmount.c +++ b/fuse/guestmount.c @@ -621,8 +621,14 @@ fg_read (const char *path, char *buf, size_t size, off_t offset, if (size> limit) size = limit;
+ /* Note the correct error handling here is tricky, because in the + * case where the call returns a zero-length buffer, it might return + * NULL. However it won't adjust rsize along the error path, so we + * can set rsize to something beforehand and use that as a flag. + */ + rsize = 1; r = guestfs_pread (g, path, size, offset,&rsize); - if (r == NULL) + if (rsize == 1&& r == NULL) return error ();
/* This should never happen, but at least it stops us overflowing -- 1.6.5.2
I've stared at this til my head hurts, and I don't get it. Why is 1 not a valid returned size from guestfs_pread? Why is NULL not an indicator of failure?
1 is a valid result size, but the only way to get rsize of 1 (assuming the prior initialization) and *also* r == NULL is in this failure case.
+ if (rsize == 1&& r == NULL) (oops, spacing: should be if (rsize == 1 && r == NULL)
The documentation says:
This function returns a buffer, or NULL on error. The size of the returned buffer is written to *size_r. The caller must free the returned buffer after use.
If NULL isn't sufficient to indicate error, guestfs_pread needs to be fixed, not this funtion.
Right. The API is too hard to use properly. An alternative is to make it so guestfs_pread returns a non-NULL pointer to one malloc'd (and unused) byte for an empty read, and also sets *rsize to 0. *THEN* a NULL return value could suffice to indicate failure.