> "As I mentioned earlier": where?
> Otherwise this description will be of
> little relevance in 5 years from now in our history.

I explained the motivation on the first revision of the patch

On Wed, May 7, 2025, 14:58 Philippe Mathieu-Daudé <phi...@linaro.org> wrote:

> Hi Elisha,
>
> On 6/5/25 18:44, Elisha Hollander wrote:
> >  > As I mentioned earlier, let's say you don't initialize the vertical
>
> "As I mentioned earlier": where? Otherwise this description will be of
> little relevance in 5 years from now in our history.
>
> > display end registers, and set the minimum scanline register, the
> > emulation will then have to allocate some display buffer, but because
> > the vertical display end is initilized as 0 the buffer will be empty and
>
> Typo "initialized".
>
> > the program break.
> >
> > Signed-off-by: donno2048 <just4now666...@gmail.com
> > <mailto:just4now666...@gmail.com>>
> >
> > ---
> >   util/memfd.c | 10 ++++++----
> >   1 file changed, 6 insertions(+), 4 deletions(-)
> >
> > diff --git a/util/memfd.c b/util/memfd.c
> > index 8a2e906..e96e5af 100644
> > --- a/util/memfd.c
> > +++ b/util/memfd.c
> > @@ -108,7 +108,7 @@ err:
> >   void *qemu_memfd_alloc(const char *name, size_t size, unsigned int
> seals,
> >                          int *fd, Error **errp)
> >   {
> > -    void *ptr;
> > +    void *ptr = NULL;
> >       int mfd = qemu_memfd_create(name, size, false, 0, seals, NULL);
> >
> >       /* some systems have memfd without sealing */
> > @@ -131,9 +131,11 @@ void *qemu_memfd_alloc(const char *name, size_t
> > size, unsigned int seals,
> >           }
> >       }
> >
> > -    ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 0);
> > -    if (ptr == MAP_FAILED) {
> > -        goto err;
> > +    if (size != 0) {
> > +        ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 0);
> > +        if (ptr == MAP_FAILED) {
> > +            goto err;
> > +        }
> >       }
> >
> >       *fd = mfd;
> > --
> > 2.30.2
>
> Alternatively always set *fd, removing the @err label:
>
> -- >8 --
> @@ -132,0 +133 @@ void *qemu_memfd_alloc(const char *name, size_t size,
> unsigned int seals,
> +    *fd = mfd;
> @@ -134,3 +135,2 @@ void *qemu_memfd_alloc(const char *name, size_t
> size, unsigned int seals,
> -    ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 0);
> -    if (ptr == MAP_FAILED) {
> -        goto err;
> +    if (!size) {
> +        return NULL;
> @@ -139,2 +139,4 @@ void *qemu_memfd_alloc(const char *name, size_t
> size, unsigned int seals,
> -    *fd = mfd;
> -    return ptr;
> +    ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 0);
> +    if (ptr != MAP_FAILED) {
> +        return ptr;
> +    }
> @@ -142 +143,0 @@ void *qemu_memfd_alloc(const char *name, size_t size,
> unsigned int seals,
> -err:
> ---
>
> Or more similar to your approach:
>
> -- >8 --
> @@ -111 +111 @@ void *qemu_memfd_alloc(const char *name, size_t size,
> unsigned int seals,
> -    void *ptr;
> +    void *ptr = NULL;
> @@ -134,5 +133,0 @@ void *qemu_memfd_alloc(const char *name, size_t
> size, unsigned int seals,
> -    ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 0);
> -    if (ptr == MAP_FAILED) {
> -        goto err;
> -    }
> -
> @@ -140 +134,0 @@ void *qemu_memfd_alloc(const char *name, size_t size,
> unsigned int seals,
> -    return ptr;
> @@ -142,4 +136,2 @@ void *qemu_memfd_alloc(const char *name, size_t
> size, unsigned int seals,
> -err:
> -    error_setg_errno(errp, errno, "failed to allocate shared memory");
> -    if (mfd >= 0) {
> -        close(mfd);
> +    if (!size) {
> +        return NULL;
> @@ -147 +139,11 @@ err:
> -    return NULL;
> +
> +    if (size) {
> +        ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, mfd, 0);
> +        if (ptr == MAP_FAILED) {
> +            error_setg_errno(errp, errno, "failed to allocate shared
> memory");
> +            if (mfd >= 0) {
> +                close(mfd);
> +            }
> +        }
> +    }
> +    return ptr;
> ---
>

Reply via email to