Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20230524213133.GD4163@brightrain.aerifal.cx>
Date: Wed, 24 May 2023 17:31:34 -0400
From: Rich Felker <dalias@...c.org>
To: Jens Gustedt <Jens.Gustedt@...ia.fr>
Cc: musl@...ts.openwall.com
Subject: Re: [C23 new stdlib 1/4] C23: add the new interfaces
 free_sized and free_aligned_sized for stdlib.h

On Wed, May 24, 2023 at 09:38:51PM +0200, Jens Gustedt wrote:
> For the moment, these are just trivial wrappers that ignored their
> parameters other than the pointer.
> 
> The names were not previously reserved, so they could generate naming
> conflicts with application code.
> 
> The "implementation" is just a trivial wrapper around free. This could
> eventually replaced by implementations that are more efficient than
> that.
> ---
>  include/stdlib.h  |  2 ++
>  src/malloc/free.c | 10 ++++++++++
>  2 files changed, 12 insertions(+)
> 
> diff --git a/include/stdlib.h b/include/stdlib.h
> index 8a873f03..7800074d 100644
> --- a/include/stdlib.h
> +++ b/include/stdlib.h
> @@ -41,6 +41,8 @@ void *malloc (size_t);
>  void *calloc (size_t, size_t);
>  void *realloc (void *, size_t);
>  void free (void *);
> +void free_sized (void *, size_t);
> +void free_aligned_sized (void *, size_t, size_t);
>  void *aligned_alloc(size_t, size_t);
>  
>  __noreturn void abort (void);
> diff --git a/src/malloc/free.c b/src/malloc/free.c
> index 3944f7b2..2b7438bc 100644
> --- a/src/malloc/free.c
> +++ b/src/malloc/free.c
> @@ -4,3 +4,13 @@ void free(void *p)
>  {
>  	__libc_free(p);
>  }
> +
> +void free_sized (void *p, size_t size)
> +{
> +	__libc_free(p);
> +}
> +
> +void free_aligned_sized (void *p, size_t alignment, size_t size)
> +{
> +	__libc_free(p);
> +}
> -- 
> 2.34.1

These really should be in a separate file or files calling free() not
__libc_free, since if free has been replaced, they should call that,
not the libc-internal one. (Imagine a program linked or LD_PRELOADed
with an alternate malloc implementation that's not C23-aware.)

Optionally, they could also evaluate the predicate to determine if
malloc has been replaced, and if not, do the actual check. The
alignment check is trivial and malloc-agnostic. The size check would
require adding a libc-internal way to access malloc_usable_size. But
this can all be done later if desired.

Rich

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.