public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] malloc: preserve errno on mcheck hooks [BZ #17924]
@ 2020-12-29 20:24 Adhemerval Zanella
  2020-12-29 21:11 ` H.J. Lu
  2020-12-29 21:12 ` Paul Eggert
  0 siblings, 2 replies; 3+ messages in thread
From: Adhemerval Zanella @ 2020-12-29 20:24 UTC (permalink / raw)
  To: libc-alpha, Paul Eggert

Similar to the fix 69fda43b8d, save and restore errno for the hook
functions used for MALLOC_CHECK_=3.

It fixes the malloc/tst-free-errno-mcheck regression.

Checked on x86_64-linux-gnu.
---
 malloc/hooks.c | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)

diff --git a/malloc/hooks.c b/malloc/hooks.c
index 6474ba8b38..fc285f15c6 100644
--- a/malloc/hooks.c
+++ b/malloc/hooks.c
@@ -260,6 +260,8 @@ free_check (void *mem, const void *caller)
   if (!mem)
     return;
 
+  int err = errno;
+
 #ifdef USE_MTAG
   /* Quickly check that the freed pointer matches the tag for the memory.
      This gives a useful double-free detection.  */
@@ -274,12 +276,16 @@ free_check (void *mem, const void *caller)
     {
       __libc_lock_unlock (main_arena.mutex);
       munmap_chunk (p);
-      return;
     }
-  /* Mark the chunk as belonging to the library again.  */
-  (void)TAG_REGION (chunk2rawmem (p), CHUNK_AVAILABLE_SIZE (p) - CHUNK_HDR_SZ);
-  _int_free (&main_arena, p, 1);
-  __libc_lock_unlock (main_arena.mutex);
+  else
+    {
+      /* Mark the chunk as belonging to the library again.  */
+      (void)TAG_REGION (chunk2rawmem (p), CHUNK_AVAILABLE_SIZE (p)
+                                         - CHUNK_HDR_SZ);
+      _int_free (&main_arena, p, 1);
+      __libc_lock_unlock (main_arena.mutex);
+    }
+  __set_errno (err);
 }
 
 static void *
-- 
2.25.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] malloc: preserve errno on mcheck hooks [BZ #17924]
  2020-12-29 20:24 [PATCH] malloc: preserve errno on mcheck hooks [BZ #17924] Adhemerval Zanella
@ 2020-12-29 21:11 ` H.J. Lu
  2020-12-29 21:12 ` Paul Eggert
  1 sibling, 0 replies; 3+ messages in thread
From: H.J. Lu @ 2020-12-29 21:11 UTC (permalink / raw)
  To: Adhemerval Zanella; +Cc: GNU C Library, Paul Eggert

On Tue, Dec 29, 2020 at 12:24 PM Adhemerval Zanella
<adhemerval.zanella@linaro.org> wrote:
>
> Similar to the fix 69fda43b8d, save and restore errno for the hook
> functions used for MALLOC_CHECK_=3.
>
> It fixes the malloc/tst-free-errno-mcheck regression.
>
> Checked on x86_64-linux-gnu.
> ---
>  malloc/hooks.c | 16 +++++++++++-----
>  1 file changed, 11 insertions(+), 5 deletions(-)
>
> diff --git a/malloc/hooks.c b/malloc/hooks.c
> index 6474ba8b38..fc285f15c6 100644
> --- a/malloc/hooks.c
> +++ b/malloc/hooks.c
> @@ -260,6 +260,8 @@ free_check (void *mem, const void *caller)
>    if (!mem)
>      return;
>
> +  int err = errno;
> +
>  #ifdef USE_MTAG
>    /* Quickly check that the freed pointer matches the tag for the memory.
>       This gives a useful double-free detection.  */
> @@ -274,12 +276,16 @@ free_check (void *mem, const void *caller)
>      {
>        __libc_lock_unlock (main_arena.mutex);
>        munmap_chunk (p);
> -      return;
>      }
> -  /* Mark the chunk as belonging to the library again.  */
> -  (void)TAG_REGION (chunk2rawmem (p), CHUNK_AVAILABLE_SIZE (p) - CHUNK_HDR_SZ);
> -  _int_free (&main_arena, p, 1);
> -  __libc_lock_unlock (main_arena.mutex);
> +  else
> +    {
> +      /* Mark the chunk as belonging to the library again.  */
> +      (void)TAG_REGION (chunk2rawmem (p), CHUNK_AVAILABLE_SIZE (p)
> +                                         - CHUNK_HDR_SZ);
> +      _int_free (&main_arena, p, 1);
> +      __libc_lock_unlock (main_arena.mutex);
> +    }
> +  __set_errno (err);
>  }
>
>  static void *
> --
> 2.25.1
>

LGTM.

Thanks.

-- 
H.J.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] malloc: preserve errno on mcheck hooks [BZ #17924]
  2020-12-29 20:24 [PATCH] malloc: preserve errno on mcheck hooks [BZ #17924] Adhemerval Zanella
  2020-12-29 21:11 ` H.J. Lu
@ 2020-12-29 21:12 ` Paul Eggert
  1 sibling, 0 replies; 3+ messages in thread
From: Paul Eggert @ 2020-12-29 21:12 UTC (permalink / raw)
  To: Adhemerval Zanella, libc-alpha

On 12/29/20 12:24 PM, Adhemerval Zanella wrote:
> Similar to the fix 69fda43b8d, save and restore errno for the hook
> functions used for MALLOC_CHECK_=3.
> 
> It fixes the malloc/tst-free-errno-mcheck regression.

Thanks, this patch looks good to me.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-12-29 21:12 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-29 20:24 [PATCH] malloc: preserve errno on mcheck hooks [BZ #17924] Adhemerval Zanella
2020-12-29 21:11 ` H.J. Lu
2020-12-29 21:12 ` Paul Eggert

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).