Welcome to mirror list, hosted at ThFree Co, Russian Federation.

cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/newlib
diff options
context:
space:
mode:
authorHenrik Nilsson via Newlib <newlib@sourceware.org>2023-02-17 08:56:49 +0300
committerCorinna Vinschen <corinna@vinschen.de>2023-02-27 12:54:26 +0300
commitc8397ae8171f00dcfb071130e6ea2b64aea17ded (patch)
tree7717c59212e9926abd98cbf1eaaee236a4a0f527 /newlib
parent5011c8cc48a22d9ccfc8d11a0f5cbfc0e5db73a6 (diff)
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.
Diffstat (limited to 'newlib')
-rw-r--r--newlib/libc/stdlib/nano-mallocr.c19
1 files 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