qemu-block
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH 4/9] util/oslib-win32: Return NULL on qemu_try_memalign() wit


From: Peter Maydell
Subject: Re: [PATCH 4/9] util/oslib-win32: Return NULL on qemu_try_memalign() with zero size
Date: Sun, 27 Feb 2022 12:54:04 +0000

On Sun, 27 Feb 2022 at 00:56, Richard Henderson
<richard.henderson@linaro.org> wrote:
>
> On 2/26/22 08:07, Peter Maydell wrote:
> > Currently if qemu_try_memalign() is asked to allocate 0 bytes, we assert.
> > Instead return NULL; this is in line with the posix_memalign() API,
> > and is valid to pass to _aligned_free() (which will do nothing).
> >
> > This change is a preparation for sharing the qemu_try_memalign()
> > code between Windows and POSIX -- at the moment only the Windows
> > version has the assert that size != 0.
> >
> > Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> > ---
> >   util/oslib-win32.c | 7 +++++--
> >   1 file changed, 5 insertions(+), 2 deletions(-)
> >
> > diff --git a/util/oslib-win32.c b/util/oslib-win32.c
> > index 05857414695..8c1c64719d7 100644
> > --- a/util/oslib-win32.c
> > +++ b/util/oslib-win32.c
> > @@ -48,13 +48,16 @@ void *qemu_try_memalign(size_t alignment, size_t size)
> >   {
> >       void *ptr;
> >
> > -    g_assert(size != 0);
> >       if (alignment < sizeof(void *)) {
> >           alignment = sizeof(void *);
> >       } else {
> >           g_assert(is_power_of_2(alignment));
> >       }
> > -    ptr = _aligned_malloc(size, alignment);
> > +    if (size) {
> > +        ptr = _aligned_malloc(size, alignment);
> > +    } else {
> > +        ptr = NULL;
> > +    }
>
> Oh, should we set errno to something here?
> Otherwise a random value will be used by qemu_memalign.

Yeah, I guess so, though the errno to use isn't obvious. Maybe EINVAL?

The alternative would be to try to audit all the callsites to
confirm they don't ever try to allocate 0 bytes and then have
the assert for both Windows and POSIX versions...

-- PMM



reply via email to

[Prev in Thread] Current Thread [Next in Thread]