diff options
author | Peter Maydell <peter.maydell@linaro.org> | 2022-02-26 18:07:18 +0000 |
---|---|---|
committer | Peter Maydell <peter.maydell@linaro.org> | 2022-03-07 13:14:07 +0000 |
commit | bc0fecc1c2f2c70780e38b3f821dc5b89eed0716 (patch) | |
tree | edcf3e2d80255a67460e4dca53467c3d20b3b8c4 /util/oslib-posix.c | |
parent | ac8057a11b81195f22602e2f0fa720baed79a41e (diff) | |
download | qemu-bc0fecc1c2f2c70780e38b3f821dc5b89eed0716.zip qemu-bc0fecc1c2f2c70780e38b3f821dc5b89eed0716.tar.gz qemu-bc0fecc1c2f2c70780e38b3f821dc5b89eed0716.tar.bz2 |
util: Return valid allocation for qemu_try_memalign() with zero size
Currently qemu_try_memalign()'s behaviour if asked to allocate
0 bytes is rather variable:
* on Windows, we will assert
* on POSIX platforms, we get the underlying behaviour of
the posix_memalign() or equivalent function, which may be
either "return a valid non-NULL pointer" or "return NULL"
Explictly check for 0 byte allocations, so we get consistent
behaviour across platforms. We handle them by incrementing the size
so that we return a valid non-NULL pointer that can later be passed
to qemu_vfree(). This is permitted behaviour for the
posix_memalign() API and is the most usual way that underlying
malloc() etc implementations handle a zero-sized allocation request,
because it won't trip up calling code that assumes NULL means an
error. (This includes our own qemu_memalign(), which will abort on
NULL.)
This change is a preparation for sharing the qemu_try_memalign() code
between Windows and POSIX.
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Reviewed-by: Richard Henderson <richard.henderson@linaro.org>
Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Diffstat (limited to 'util/oslib-posix.c')
-rw-r--r-- | util/oslib-posix.c | 3 |
1 files changed, 3 insertions, 0 deletions
diff --git a/util/oslib-posix.c b/util/oslib-posix.c index 0278902..f7e22f4 100644 --- a/util/oslib-posix.c +++ b/util/oslib-posix.c @@ -209,6 +209,9 @@ void *qemu_try_memalign(size_t alignment, size_t size) g_assert(is_power_of_2(alignment)); } + if (size == 0) { + size++; + } #if defined(CONFIG_POSIX_MEMALIGN) int ret; ret = posix_memalign(&ptr, alignment, size); |