* [PATCH] malloc: Simplify checked_request2size interface
@ 2022-06-28 11:45 Florian Weimer
2022-06-28 15:26 ` Siddhesh Poyarekar
0 siblings, 1 reply; 2+ messages in thread
From: Florian Weimer @ 2022-06-28 11:45 UTC (permalink / raw)
To: libc-alpha
In-band signaling avoids an uninitialized variable warning when
building with -Og and GCC 12.
Tested on i686-linux-gnu and x86_64-linux-gnu. Built with
build-many-glibcs.py.
---
malloc/malloc-check.c | 3 ++-
malloc/malloc.c | 30 ++++++++++++++++--------------
2 files changed, 18 insertions(+), 15 deletions(-)
diff --git a/malloc/malloc-check.c b/malloc/malloc-check.c
index 0299fe99a7..3368532adf 100644
--- a/malloc/malloc-check.c
+++ b/malloc/malloc-check.c
@@ -275,7 +275,8 @@ realloc_check (void *oldmem, size_t bytes)
malloc_printerr ("realloc(): invalid pointer");
const INTERNAL_SIZE_T oldsize = chunksize (oldp);
- if (!checked_request2size (rb, &chnb))
+ chnb = checked_request2size (rb);
+ if (chnb == 0)
{
__set_errno (ENOMEM);
goto invert;
diff --git a/malloc/malloc.c b/malloc/malloc.c
index 09e5ff2bce..12908b8f97 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -1333,15 +1333,15 @@ nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
MINSIZE : \
((req) + SIZE_SZ + MALLOC_ALIGN_MASK) & ~MALLOC_ALIGN_MASK)
-/* Check if REQ overflows when padded and aligned and if the resulting value
- is less than PTRDIFF_T. Returns TRUE and the requested size or MINSIZE in
- case the value is less than MINSIZE on SZ or false if any of the previous
- check fail. */
-static inline bool
-checked_request2size (size_t req, size_t *sz) __nonnull (1)
+/* Check if REQ overflows when padded and aligned and if the resulting
+ value is less than PTRDIFF_T. Returns the requested size or
+ MINSIZE in case the value is less than MINSIZE, or 0 if any of the
+ previous checks fail. */
+static inline size_t
+checked_request2size (size_t req) __nonnull (1)
{
if (__glibc_unlikely (req > PTRDIFF_MAX))
- return false;
+ return 0;
/* When using tagged memory, we cannot share the end of the user
block with the header for the next chunk, so ensure that we
@@ -1359,8 +1359,7 @@ checked_request2size (size_t req, size_t *sz) __nonnull (1)
~(size_t)(__MTAG_GRANULE_SIZE - 1);
}
- *sz = request2size (req);
- return true;
+ return request2size (req);
}
/*
@@ -3295,8 +3294,8 @@ __libc_malloc (size_t bytes)
ptmalloc_init ();
#if USE_TCACHE
/* int_free also calls request2size, be careful to not pad twice. */
- size_t tbytes;
- if (!checked_request2size (bytes, &tbytes))
+ size_t tbytes = checked_request2size (bytes);
+ if (tbytes == 0)
{
__set_errno (ENOMEM);
return NULL;
@@ -3443,7 +3442,8 @@ __libc_realloc (void *oldmem, size_t bytes)
|| __builtin_expect (misaligned_chunk (oldp), 0)))
malloc_printerr ("realloc(): invalid pointer");
- if (!checked_request2size (bytes, &nb))
+ nb = checked_request2size (bytes);
+ if (nb == 0)
{
__set_errno (ENOMEM);
return NULL;
@@ -3800,7 +3800,8 @@ _int_malloc (mstate av, size_t bytes)
aligned.
*/
- if (!checked_request2size (bytes, &nb))
+ nb = checked_request2size (bytes);
+ if (nb == 0)
{
__set_errno (ENOMEM);
return NULL;
@@ -4952,7 +4953,8 @@ _int_memalign (mstate av, size_t alignment, size_t bytes)
- if (!checked_request2size (bytes, &nb))
+ nb = checked_request2size (bytes);
+ if (nb == 0)
{
__set_errno (ENOMEM);
return NULL;
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] malloc: Simplify checked_request2size interface
2022-06-28 11:45 [PATCH] malloc: Simplify checked_request2size interface Florian Weimer
@ 2022-06-28 15:26 ` Siddhesh Poyarekar
0 siblings, 0 replies; 2+ messages in thread
From: Siddhesh Poyarekar @ 2022-06-28 15:26 UTC (permalink / raw)
To: Florian Weimer, libc-alpha
On 28/06/2022 17:15, Florian Weimer via Libc-alpha wrote:
> In-band signaling avoids an uninitialized variable warning when
> building with -Og and GCC 12.
>
> Tested on i686-linux-gnu and x86_64-linux-gnu. Built with
> build-many-glibcs.py.
LGTM.
Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
>
> ---
> malloc/malloc-check.c | 3 ++-
> malloc/malloc.c | 30 ++++++++++++++++--------------
> 2 files changed, 18 insertions(+), 15 deletions(-)
>
> diff --git a/malloc/malloc-check.c b/malloc/malloc-check.c
> index 0299fe99a7..3368532adf 100644
> --- a/malloc/malloc-check.c
> +++ b/malloc/malloc-check.c
> @@ -275,7 +275,8 @@ realloc_check (void *oldmem, size_t bytes)
> malloc_printerr ("realloc(): invalid pointer");
> const INTERNAL_SIZE_T oldsize = chunksize (oldp);
>
> - if (!checked_request2size (rb, &chnb))
> + chnb = checked_request2size (rb);
> + if (chnb == 0)
> {
> __set_errno (ENOMEM);
> goto invert;
> diff --git a/malloc/malloc.c b/malloc/malloc.c
> index 09e5ff2bce..12908b8f97 100644
> --- a/malloc/malloc.c
> +++ b/malloc/malloc.c
> @@ -1333,15 +1333,15 @@ nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
> MINSIZE : \
> ((req) + SIZE_SZ + MALLOC_ALIGN_MASK) & ~MALLOC_ALIGN_MASK)
>
> -/* Check if REQ overflows when padded and aligned and if the resulting value
> - is less than PTRDIFF_T. Returns TRUE and the requested size or MINSIZE in
> - case the value is less than MINSIZE on SZ or false if any of the previous
> - check fail. */
> -static inline bool
> -checked_request2size (size_t req, size_t *sz) __nonnull (1)
> +/* Check if REQ overflows when padded and aligned and if the resulting
> + value is less than PTRDIFF_T. Returns the requested size or
> + MINSIZE in case the value is less than MINSIZE, or 0 if any of the
> + previous checks fail. */
> +static inline size_t
> +checked_request2size (size_t req) __nonnull (1)
> {
> if (__glibc_unlikely (req > PTRDIFF_MAX))
> - return false;
> + return 0;
>
> /* When using tagged memory, we cannot share the end of the user
> block with the header for the next chunk, so ensure that we
> @@ -1359,8 +1359,7 @@ checked_request2size (size_t req, size_t *sz) __nonnull (1)
> ~(size_t)(__MTAG_GRANULE_SIZE - 1);
> }
>
> - *sz = request2size (req);
> - return true;
> + return request2size (req);
> }
>
> /*
> @@ -3295,8 +3294,8 @@ __libc_malloc (size_t bytes)
> ptmalloc_init ();
> #if USE_TCACHE
> /* int_free also calls request2size, be careful to not pad twice. */
> - size_t tbytes;
> - if (!checked_request2size (bytes, &tbytes))
> + size_t tbytes = checked_request2size (bytes);
> + if (tbytes == 0)
> {
> __set_errno (ENOMEM);
> return NULL;
> @@ -3443,7 +3442,8 @@ __libc_realloc (void *oldmem, size_t bytes)
> || __builtin_expect (misaligned_chunk (oldp), 0)))
> malloc_printerr ("realloc(): invalid pointer");
>
> - if (!checked_request2size (bytes, &nb))
> + nb = checked_request2size (bytes);
> + if (nb == 0)
> {
> __set_errno (ENOMEM);
> return NULL;
> @@ -3800,7 +3800,8 @@ _int_malloc (mstate av, size_t bytes)
> aligned.
> */
>
> - if (!checked_request2size (bytes, &nb))
> + nb = checked_request2size (bytes);
> + if (nb == 0)
> {
> __set_errno (ENOMEM);
> return NULL;
> @@ -4952,7 +4953,8 @@ _int_memalign (mstate av, size_t alignment, size_t bytes)
>
>
>
> - if (!checked_request2size (bytes, &nb))
> + nb = checked_request2size (bytes);
> + if (nb == 0)
> {
> __set_errno (ENOMEM);
> return NULL;
>
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2022-06-28 15:27 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-28 11:45 [PATCH] malloc: Simplify checked_request2size interface Florian Weimer
2022-06-28 15:26 ` Siddhesh Poyarekar
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 read-only IMAP folder(s) and NNTP newsgroup(s).