mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: Natanael Copa <ncopa@alpinelinux.org>
Cc: "Alex Xu (Hello71)" <alex_y_xu@yahoo.ca>, musl@lists.openwall.com
Subject: Re: [musl] [PATCH] don't set errno in free
Date: Thu, 21 Jan 2021 11:18:08 -0500	[thread overview]
Message-ID: <20210121161807.GN23432@brightrain.aerifal.cx> (raw)
In-Reply-To: <20210121165000.61205767@ncopa-desktop.lan>

On Thu, Jan 21, 2021 at 04:50:00PM +0100, Natanael Copa wrote:
> On Thu, 21 Jan 2021 09:02:40 -0500
> "Alex Xu (Hello71)" <alex_y_xu@yahoo.ca> wrote:
> 
> > busybox echo fails if free sets errno, which madvise does on old
> > kernels.
> > ---
> >  src/malloc/mallocng/free.c | 14 ++++++++++++--
> >  1 file changed, 12 insertions(+), 2 deletions(-)
> > 
> > diff --git a/src/malloc/mallocng/free.c b/src/malloc/mallocng/free.c
> > index 40745f97..82836815 100644
> > --- a/src/malloc/mallocng/free.c
> > +++ b/src/malloc/mallocng/free.c
> > @@ -119,7 +119,13 @@ void free(void *p)
> >  	if (((uintptr_t)(start-1) ^ (uintptr_t)end) >= 2*PGSZ && g->last_idx) {
> >  		unsigned char *base = start + (-(uintptr_t)start & (PGSZ-1));
> >  		size_t len = (end-base) & -PGSZ;
> > -		if (len) madvise(base, len, MADV_FREE);
> > +		if (len) {
> > +			// madvise(..., MADV_FREE) returns -EINVAL on old kernels
> > +			// POSIX.1-202x requires free() to not modify errno on success
> > +			int e = errno;
> > +			madvise(base, len, MADV_FREE);
> > +			errno = e;
> > +		}
> >  	}
> 
> I think we should save the errno early and make sure its restored on
> exit of the function. you should also include <errno.h>. I suggest
> something like:
> 
> diff --git a/src/malloc/mallocng/free.c b/src/malloc/mallocng/free.c
> index 40745f97..77bed88b 100644
> --- a/src/malloc/mallocng/free.c
> +++ b/src/malloc/mallocng/free.c
> @@ -1,6 +1,7 @@
>  #define _BSD_SOURCE
>  #include <stdlib.h>
>  #include <sys/mman.h>
> +#include <errno.h>
>  
>  #include "meta.h"
>  
> @@ -102,6 +103,7 @@ void free(void *p)
>  {
>         if (!p) return;
>  
> +       int orig_errno = errno;

This is much costlier. It puts the TLS access (faulting and emulating
on old MIPS) in the path that runs on every call.

Rich

  reply	other threads:[~2021-01-21 16:18 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20210121140240.83405-1-alex_y_xu.ref@yahoo.ca>
2021-01-21 14:02 ` Alex Xu (Hello71)
2021-01-21 15:50   ` Natanael Copa
2021-01-21 16:18     ` Rich Felker [this message]
2021-01-21 16:20       ` Florian Weimer
2021-01-21 16:31       ` Natanael Copa
2021-01-21 16:27   ` Rich Felker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210121161807.GN23432@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=alex_y_xu@yahoo.ca \
    --cc=musl@lists.openwall.com \
    --cc=ncopa@alpinelinux.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).