Commit d644f8beaa268a4e36b473f283f0b5a5ff66d8c2

Authored by malc
1 parent e8d2a887

Add out of memory and zero size argument checks to be consistent with

the qemu_malloc routines
Showing 1 changed file with 25 additions and 5 deletions
@@ -51,10 +51,23 @@ @@ -51,10 +51,23 @@
51 #include "sysemu.h" 51 #include "sysemu.h"
52 #include "qemu_socket.h" 52 #include "qemu_socket.h"
53 53
  54 +#if !defined(_POSIX_C_SOURCE) || defined(_WIN32)
  55 +static void *oom_check(void *ptr)
  56 +{
  57 + if (ptr == NULL) {
  58 + abort();
  59 + }
  60 + return ptr;
  61 +}
  62 +#endif
  63 +
54 #if defined(_WIN32) 64 #if defined(_WIN32)
55 void *qemu_memalign(size_t alignment, size_t size) 65 void *qemu_memalign(size_t alignment, size_t size)
56 { 66 {
57 - return VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE); 67 + if (!size) {
  68 + abort();
  69 + }
  70 + return oom_check(VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE));
58 } 71 }
59 72
60 void *qemu_vmalloc(size_t size) 73 void *qemu_vmalloc(size_t size)
@@ -62,7 +75,10 @@ void *qemu_vmalloc(size_t size) @@ -62,7 +75,10 @@ void *qemu_vmalloc(size_t size)
62 /* FIXME: this is not exactly optimal solution since VirtualAlloc 75 /* FIXME: this is not exactly optimal solution since VirtualAlloc
63 has 64Kb granularity, but at least it guarantees us that the 76 has 64Kb granularity, but at least it guarantees us that the
64 memory is page aligned. */ 77 memory is page aligned. */
65 - return VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE); 78 + if (!size) {
  79 + abort();
  80 + }
  81 + return oom_check(VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE));
66 } 82 }
67 83
68 void qemu_vfree(void *ptr) 84 void qemu_vfree(void *ptr)
@@ -106,6 +122,10 @@ static void *kqemu_vmalloc(size_t size) @@ -106,6 +122,10 @@ static void *kqemu_vmalloc(size_t size)
106 struct statfs stfs; 122 struct statfs stfs;
107 #endif 123 #endif
108 124
  125 + if (!size) {
  126 + abort ();
  127 + }
  128 +
109 if (phys_ram_fd < 0) { 129 if (phys_ram_fd < 0) {
110 tmpdir = getenv("QEMU_TMPDIR"); 130 tmpdir = getenv("QEMU_TMPDIR");
111 if (!tmpdir) 131 if (!tmpdir)
@@ -188,12 +208,12 @@ void *qemu_memalign(size_t alignment, size_t size) @@ -188,12 +208,12 @@ void *qemu_memalign(size_t alignment, size_t size)
188 void *ptr; 208 void *ptr;
189 ret = posix_memalign(&ptr, alignment, size); 209 ret = posix_memalign(&ptr, alignment, size);
190 if (ret != 0) 210 if (ret != 0)
191 - return NULL; 211 + abort();
192 return ptr; 212 return ptr;
193 #elif defined(HOST_BSD) 213 #elif defined(HOST_BSD)
194 - return valloc(size); 214 + return oom_check(valloc(size));
195 #else 215 #else
196 - return memalign(alignment, size); 216 + return oom_check(memalign(alignment, size));
197 #endif 217 #endif
198 } 218 }
199 219