Skip to content

Commit

Permalink
* malloc/malloc.c (sYSMALLOc): Avoid infinite loop if MMAP
Browse files Browse the repository at this point in the history
	keeps failing and heap growth or new heap creation isn't
	successful either.
	* malloc/tst-malloc.c (main): Add new tests.
  • Loading branch information
Ulrich Drepper committed Aug 24, 2006
1 parent 542a6f6 commit 7463d5c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
7 changes: 7 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
2006-08-24 Jakub Jelinek <jakub@redhat.com>

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

2006-08-24 Ulrich Drepper <drepper@redhat.com>

[BZ #2734]
Expand Down
4 changes: 3 additions & 1 deletion malloc/malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)) {
Expand Down Expand Up @@ -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;

Expand Down
12 changes: 11 additions & 1 deletion malloc/tst-malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ merror (const char *msg)
int
main (void)
{
void *p;
void *p, *q;
int save;

errno = 0;
Expand Down Expand Up @@ -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;
}

0 comments on commit 7463d5c

Please sign in to comment.