about summary refs log tree commit diff
path: root/malloc
diff options
context:
space:
mode:
authorUlrich Drepper <drepper@redhat.com>2006-08-24 17:30:37 +0000
committerUlrich Drepper <drepper@redhat.com>2006-08-24 17:30:37 +0000
commit7463d5cb4d42ffa944f05415f9d320b1b64d2bcf (patch)
tree5965915308653b2ce43950fc61101339c8149c1b /malloc
parent542a6f62afe483fe282adaf60f5c0a185c12e824 (diff)
downloadglibc-7463d5cb4d42ffa944f05415f9d320b1b64d2bcf.tar.gz
glibc-7463d5cb4d42ffa944f05415f9d320b1b64d2bcf.tar.xz
glibc-7463d5cb4d42ffa944f05415f9d320b1b64d2bcf.zip
* malloc/malloc.c (sYSMALLOc): Avoid infinite loop if MMAP
	keeps failing and heap growth or new heap creation isn't
	successful either.
	* malloc/tst-malloc.c (main): Add new tests.
Diffstat (limited to 'malloc')
-rw-r--r--malloc/malloc.c4
-rw-r--r--malloc/tst-malloc.c12
2 files changed, 14 insertions, 2 deletions
diff --git a/malloc/malloc.c b/malloc/malloc.c
index 02f659708d..6de1409c4f 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -2860,6 +2860,7 @@ static Void_t* sYSMALLOc(nb, av) INTERNAL_SIZE_T nb; mstate av;
   unsigned long   sum;            /* for updating stats */
 
   size_t          pagemask  = mp_.pagesize - 1;
+  bool            tried_mmap = false;
 
 
 #if HAVE_MMAP
@@ -2883,6 +2884,7 @@ static Void_t* sYSMALLOc(nb, av) INTERNAL_SIZE_T nb; mstate av;
       is no following chunk whose prev_size field could be used.
     */
     size = (nb + SIZE_SZ + MALLOC_ALIGN_MASK + pagemask) & ~pagemask;
+    tried_mmap = true;
 
     /* Don't try if size wraps around 0 */
     if ((unsigned long)(size) > (unsigned long)(nb)) {
@@ -3006,7 +3008,7 @@ static Void_t* sYSMALLOc(nb, av) INTERNAL_SIZE_T nb; mstate av;
 	set_foot(old_top, (old_size + 2*SIZE_SZ));
       }
     }
-    else
+    else if (!tried_mmap)
       /* We can at least try to use to mmap memory.  */
       goto try_mmap;
 
diff --git a/malloc/tst-malloc.c b/malloc/tst-malloc.c
index d555ae46ef..81d279236c 100644
--- a/malloc/tst-malloc.c
+++ b/malloc/tst-malloc.c
@@ -33,7 +33,7 @@ merror (const char *msg)
 int
 main (void)
 {
-  void *p;
+  void *p, *q;
   int save;
 
   errno = 0;
@@ -64,5 +64,15 @@ main (void)
   if (p != NULL)
     merror ("realloc (p, 0) failed.");
 
+  p = malloc (513 * 1024);
+  if (p == NULL)
+    merror ("malloc (513K) failed.");
+
+  q = malloc (-512 * 1024);
+  if (q != NULL)
+    merror ("malloc (-512K) succeeded.");
+
+  free (p);
+
   return errors != 0;
 }