public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] nano-mallocr: Prevent NULL pointer de-reference in free_list
@ 2023-02-17  5:56 Henrik Nilsson
  2023-02-23  6:59 ` Henrik Nilsson
  0 siblings, 1 reply; 3+ messages in thread
From: Henrik Nilsson @ 2023-02-17  5:56 UTC (permalink / raw)
  To: newlib; +Cc: Henrik Nilsson

The existing code checked if there was a chunk in free_list and
that the tail was not the next chunk.

The check if there is a chunk is not needed since it's already
known but the case of a single chunk in free_list needs to be
handled differently.
---
 newlib/libc/stdlib/nano-mallocr.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/newlib/libc/stdlib/nano-mallocr.c b/newlib/libc/stdlib/nano-mallocr.c
index b2273ba60..a2b50facc 100644
--- a/newlib/libc/stdlib/nano-mallocr.c
+++ b/newlib/libc/stdlib/nano-mallocr.c
@@ -333,14 +333,23 @@ void * nano_malloc(RARG malloc_size_t s)
                {
                    p->size += alloc_size;
 
-                   /* Remove chunk from free_list */
+                   /* Remove chunk from free_list. Since p != NULL there is
+                      at least one chunk */
                    r = free_list;
-                   while (r && p != r->next)
+                   if (r->next == NULL)
                    {
-                     r = r->next;
+                       /* There is only a single chunk, remove it */
+                       free_list = NULL;
+                   }
+                   else
+                   {
+                       /* Search for the chunk before the one to be removed */
+                       while (p != r->next)
+                       {
+                           r = r->next;
+                       }
+                       r->next = NULL;
                    }
-                   r->next = NULL;
-
                    r = p;
                }
                else
-- 
2.34.1


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

* Re: [PATCH] nano-mallocr: Prevent NULL pointer de-reference in free_list
  2023-02-17  5:56 [PATCH] nano-mallocr: Prevent NULL pointer de-reference in free_list Henrik Nilsson
@ 2023-02-23  6:59 ` Henrik Nilsson
  2023-02-27 10:00   ` Corinna Vinschen
  0 siblings, 1 reply; 3+ messages in thread
From: Henrik Nilsson @ 2023-02-23  6:59 UTC (permalink / raw)
  To: newlib

On Fri, Feb 17, 2023 at 06:56:49AM +0100, Henrik Nilsson wrote:
> The existing code checked if there was a chunk in free_list and
> that the tail was not the next chunk.
> 
> The check if there is a chunk is not needed since it's already
> known but the case of a single chunk in free_list needs to be
> handled differently.
> ---
>  newlib/libc/stdlib/nano-mallocr.c | 19 ++++++++++++++-----
>  1 file changed, 14 insertions(+), 5 deletions(-)
> 
> diff --git a/newlib/libc/stdlib/nano-mallocr.c b/newlib/libc/stdlib/nano-mallocr.c
> index b2273ba60..a2b50facc 100644
> --- a/newlib/libc/stdlib/nano-mallocr.c
> +++ b/newlib/libc/stdlib/nano-mallocr.c
> @@ -333,14 +333,23 @@ void * nano_malloc(RARG malloc_size_t s)
>                 {
>                     p->size += alloc_size;
>  
> -                   /* Remove chunk from free_list */
> +                   /* Remove chunk from free_list. Since p != NULL there is
> +                      at least one chunk */
>                     r = free_list;
> -                   while (r && p != r->next)
> +                   if (r->next == NULL)
>                     {
> -                     r = r->next;
> +                       /* There is only a single chunk, remove it */
> +                       free_list = NULL;
> +                   }
> +                   else
> +                   {
> +                       /* Search for the chunk before the one to be removed */
> +                       while (p != r->next)
> +                       {
> +                           r = r->next;
> +                       }
> +                       r->next = NULL;
>                     }
> -                   r->next = NULL;
> -
>                     r = p;
>                 }
>                 else
> -- 
> 2.34.1
>
Any comment on this patch?

BR,
Henrik

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

* Re: [PATCH] nano-mallocr: Prevent NULL pointer de-reference in free_list
  2023-02-23  6:59 ` Henrik Nilsson
@ 2023-02-27 10:00   ` Corinna Vinschen
  0 siblings, 0 replies; 3+ messages in thread
From: Corinna Vinschen @ 2023-02-27 10:00 UTC (permalink / raw)
  To: Henrik Nilsson; +Cc: newlib

On Feb 23 07:59, Henrik Nilsson via Newlib wrote:
> On Fri, Feb 17, 2023 at 06:56:49AM +0100, Henrik Nilsson wrote:
> > The existing code checked if there was a chunk in free_list and
> > that the tail was not the next chunk.
> > 
> > The check if there is a chunk is not needed since it's already
> > known but the case of a single chunk in free_list needs to be
> > handled differently.
> > ---
> >  newlib/libc/stdlib/nano-mallocr.c | 19 ++++++++++++++-----
> >  1 file changed, 14 insertions(+), 5 deletions(-)
> > 
> > diff --git a/newlib/libc/stdlib/nano-mallocr.c b/newlib/libc/stdlib/nano-mallocr.c
> > index b2273ba60..a2b50facc 100644
> > --- a/newlib/libc/stdlib/nano-mallocr.c
> > +++ b/newlib/libc/stdlib/nano-mallocr.c
> > @@ -333,14 +333,23 @@ void * nano_malloc(RARG malloc_size_t s)
> >                 {
> >                     p->size += alloc_size;
> >  
> > -                   /* Remove chunk from free_list */
> > +                   /* Remove chunk from free_list. Since p != NULL there is
> > +                      at least one chunk */
> >                     r = free_list;
> > -                   while (r && p != r->next)
> > +                   if (r->next == NULL)
> >                     {
> > -                     r = r->next;
> > +                       /* There is only a single chunk, remove it */
> > +                       free_list = NULL;
> > +                   }
> > +                   else
> > +                   {
> > +                       /* Search for the chunk before the one to be removed */
> > +                       while (p != r->next)
> > +                       {
> > +                           r = r->next;
> > +                       }
> > +                       r->next = NULL;
> >                     }
> > -                   r->next = NULL;
> > -
> >                     r = p;
> >                 }
> >                 else
> > -- 
> > 2.34.1
> >
> Any comment on this patch?

Pushed.

Thanks,
Corinna


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

end of thread, other threads:[~2023-02-27 10:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-17  5:56 [PATCH] nano-mallocr: Prevent NULL pointer de-reference in free_list Henrik Nilsson
2023-02-23  6:59 ` Henrik Nilsson
2023-02-27 10:00   ` Corinna Vinschen

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