Commit d644f8beaa268a4e36b473f283f0b5a5ff66d8c2
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
osdep.c
| ... | ... | @@ -51,10 +51,23 @@ |
| 51 | 51 | #include "sysemu.h" |
| 52 | 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 | 64 | #if defined(_WIN32) |
| 55 | 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 | 73 | void *qemu_vmalloc(size_t size) |
| ... | ... | @@ -62,7 +75,10 @@ void *qemu_vmalloc(size_t size) |
| 62 | 75 | /* FIXME: this is not exactly optimal solution since VirtualAlloc |
| 63 | 76 | has 64Kb granularity, but at least it guarantees us that the |
| 64 | 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 | 84 | void qemu_vfree(void *ptr) |
| ... | ... | @@ -106,6 +122,10 @@ static void *kqemu_vmalloc(size_t size) |
| 106 | 122 | struct statfs stfs; |
| 107 | 123 | #endif |
| 108 | 124 | |
| 125 | + if (!size) { | |
| 126 | + abort (); | |
| 127 | + } | |
| 128 | + | |
| 109 | 129 | if (phys_ram_fd < 0) { |
| 110 | 130 | tmpdir = getenv("QEMU_TMPDIR"); |
| 111 | 131 | if (!tmpdir) |
| ... | ... | @@ -188,12 +208,12 @@ void *qemu_memalign(size_t alignment, size_t size) |
| 188 | 208 | void *ptr; |
| 189 | 209 | ret = posix_memalign(&ptr, alignment, size); |
| 190 | 210 | if (ret != 0) |
| 191 | - return NULL; | |
| 211 | + abort(); | |
| 192 | 212 | return ptr; |
| 193 | 213 | #elif defined(HOST_BSD) |
| 194 | - return valloc(size); | |
| 214 | + return oom_check(valloc(size)); | |
| 195 | 215 | #else |
| 196 | - return memalign(alignment, size); | |
| 216 | + return oom_check(memalign(alignment, size)); | |
| 197 | 217 | #endif |
| 198 | 218 | } |
| 199 | 219 | ... | ... |