Skip to contentSkip to conversation

Fix error code checking for SetFilePointer() call

PATCH Applied Fabien Chouteau 3 participants 4 messages Dec 10, 2012 – Dec 11, 2012 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix error code checking for SetFilePointer() call

An error has occurred if the return value is invalid_set_file_pointer and getlasterror doesn't return no_error.

---
 block/raw-win32.c |   17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
block/raw-win32.c +14 -3
index 0c05c58..ce207a3 100644
@@ -303,13 +303,24 @@ static int raw_truncate(BlockDriverState *bs, int64_t offset)
 {
     BDRVRawState *s = bs->opaque;
     LONG low, high;
+    DWORD dwPtrLow;
 
     low = offset;
     high = offset >> 32;
-    if (!SetFilePointer(s->hfile, low, &high, FILE_BEGIN))
-	return -EIO;
-    if (!SetEndOfFile(s->hfile))
+
+    /*
+     * An error has occurred if the return value is INVALID_SET_FILE_POINTER
+     * and GetLastError doesn't return NO_ERROR.
+     */
+    dwPtrLow = SetFilePointer(s->hfile, low, &high, FILE_BEGIN);
+    if (dwPtrLow == INVALID_SET_FILE_POINTER && GetLastError() != NO_ERROR) {
+        fprintf(stderr, "SetFilePointer error: %d\n", GetLastError());
+        return -EIO;
+    }
+    if (SetEndOfFile(s->hfile) == 0) {
+        fprintf(stderr, "SetEndOfFile error: %d\n", GetLastError());
         return -EIO;
+    }
     return 0;
 }
 
Signature · 1 line
1.7.9.5
Acked-by on v1 1/1
#1 lore raw
Quoted 7 lines · Fabien Chouteau

An error has occurred if the return value is invalid_set_file_pointer and getlasterror doesn't return no_error.

---
 block/raw-win32.c |   17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

The fprintf() is kind of iffy but we only return -EIO so I guess it helps to print the full error.

Applied
#2 lore raw

Am 11.12.2012 10:21, schrieb Stefan Hajnoczi:

Quoted 9 lines · Fabien Chouteau
Quoted 7 lines

An error has occurred if the return value is invalid_set_file_pointer and getlasterror doesn't return no_error.

---
 block/raw-win32.c |   17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
Quoted 4 lines

The fprintf() is kind of iffy but we only return -EIO so I guess it helps to print the full error.

Thanks, applied to the block branch.

Kevin

#3 lore raw
In reply to Stefan Hajnoczi
Quoted 2 lines · Stefan Hajnoczi

The fprintf() is kind of iffy but we only return -EIO so I guess it helps to print the full error.

I add it because it took me a while to get to the actual error. Without it the error message is even more cryptic:

qemu-system-ppc.exe: -hda fat:no-mbr:rw:.: could not open disk image fat:no-mbr:rw:.: Operation not permitted

At least it helps to find where the error comes from.

Signature · 1 line
Fabien Chouteau