Skip to contentSkip to conversation

Replace WriteFileEx with WriteFile in qemu_create_pidfile

PATCH Applied Fabien Chouteau 3 participants 3 messages Nov 7, 2011 – Nov 9, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Replace WriteFileEx with WriteFile in qemu_create_pidfile

The function that writes pidfile for win32 uses WriteFileEx which is an asynchronous IO function. The arguments given to WriteFileEx are allocated on the stack and one of them is "in out". When the IO operation is actually executed the calling function has already returned, so the arguments are no longer allocated or allocated to another frame.

---
 os-win32.c |    7 ++++---
 1 files changed, 4 insertions(+), 3 deletions(-)
os-win32.c +4 -3
index 7909401..8ad5fa1 100644
@@ -130,14 +130,15 @@ int qemu_create_pidfile(const char *filename)
     memset(&overlap, 0, sizeof(overlap));
 
     file = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL,
-		      OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
+                      OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
 
     if (file == INVALID_HANDLE_VALUE) {
         return -1;
     }
     len = snprintf(buffer, sizeof(buffer), "%d\n", getpid());
-    ret = WriteFileEx(file, (LPCVOID)buffer, (DWORD)len,
-		      &overlap, NULL);
+    ret = WriteFile(file, (LPCVOID)buffer, (DWORD)len,
+                    NULL, &overlap);
+    CloseHandle(file);
     if (ret == 0) {
         return -1;
     }
Signature · 1 line
1.7.5.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 16 lines · Fabien Chouteau

The function that writes pidfile for win32 uses WriteFileEx which is an asynchronous IO function. The arguments given to WriteFileEx are allocated on the stack and one of them is "in out". When the IO operation is actually executed the calling function has already returned, so the arguments are no longer allocated or allocated to another frame.

---
  os-win32.c |    7 ++++---
  1 files changed, 4 insertions(+), 3 deletions(-)
os-win32.c +0 -0
index 7909401..8ad5fa1 100644

os-win32.c int qemu_create_pidfile(const char *filename) · line 133

Quoted 5 lines
os-win32.c +1 -1
@@ -130,14 +130,15 @@ int qemu_create_pidfile(const char *filename)
      memset(&overlap, 0, sizeof(overlap));
 
      file = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL,
-		      OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
+                      OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

This is s/tab/space/, unrelated but fine.

os-win32.c int qemu_create_pidfile(const char *filename) · line 144

Quoted 12 lines
os-win32.c +3 -2
      if (file == INVALID_HANDLE_VALUE) {
          return -1;
      }
      len = snprintf(buffer, sizeof(buffer), "%d\n", getpid());
-    ret = WriteFileEx(file, (LPCVOID)buffer, (DWORD)len,
-		&overlap, NULL);
+    ret = WriteFile(file, (LPCVOID)buffer, (DWORD)len,
+                    NULL,&overlap);
+    CloseHandle(file);
      if (ret == 0) {
          return -1;
      }

Paolo

#2 lore raw
In reply to Fabien Chouteau
Quoted 7 lines · Fabien Chouteau

The function that writes pidfile for win32 uses WriteFileEx which is an asynchronous IO function. The arguments given to WriteFileEx are allocated on the stack and one of them is "in out". When the IO operation is actually executed the calling function has already returned, so the arguments are no longer allocated or allocated to another frame.

Applied. Thanks.

Regards,

Anthony Liguori

Quoted 27 lines
---
  os-win32.c |    7 ++++---
  1 files changed, 4 insertions(+), 3 deletions(-)
os-win32.c +4 -3
index 7909401..8ad5fa1 100644
@@ -130,14 +130,15 @@ int qemu_create_pidfile(const char *filename)
      memset(&overlap, 0, sizeof(overlap));
 
      file = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL,
-		      OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
+                      OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
 
      if (file == INVALID_HANDLE_VALUE) {
          return -1;
      }
      len = snprintf(buffer, sizeof(buffer), "%d\n", getpid());
-    ret = WriteFileEx(file, (LPCVOID)buffer, (DWORD)len,
-		&overlap, NULL);
+    ret = WriteFile(file, (LPCVOID)buffer, (DWORD)len,
+                    NULL,&overlap);
+    CloseHandle(file);
      if (ret == 0) {
          return -1;
      }