public inbox for newlib-cvs@sourceware.org
help / color / mirror / Atom feed
From: Corinna Vinschen <corinna@sourceware.org>
To: newlib-cvs@sourceware.org
Subject: [newlib-cygwin/main] nano-mallocr: Prevent NULL pointer de-reference in free_list
Date: Mon, 27 Feb 2023 09:58:20 +0000 (GMT)	[thread overview]
Message-ID: <20230227095820.A87983858D20@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=c8397ae8171f00dcfb071130e6ea2b64aea17ded

commit c8397ae8171f00dcfb071130e6ea2b64aea17ded
Author:     Henrik Nilsson via Newlib <newlib@sourceware.org>
AuthorDate: Fri Feb 17 06:56:49 2023 +0100
Commit:     Corinna Vinschen <corinna@vinschen.de>
CommitDate: Mon Feb 27 10:54:26 2023 +0100

    nano-mallocr: Prevent NULL pointer de-reference in free_list
    
    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.

Diff:
---
 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 b2273ba60943..a2b50facc35e 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

                 reply	other threads:[~2023-02-27  9:58 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20230227095820.A87983858D20@sourceware.org \
    --to=corinna@sourceware.org \
    --cc=newlib-cvs@sourceware.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.
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).