about summary refs log tree commit diff
path: root/malloc/tst-memalign.c
diff options
context:
space:
mode:
authorWill Newton <will.newton@linaro.org>2013-10-10 13:17:13 +0100
committerWill Newton <will.newton@linaro.org>2013-10-30 14:46:02 -0700
commita56ee40b176d0a3f47f2a7eb75208f2e3763c9fd (patch)
tree02a3f3bc0c86de86bc89f185a8312b9b1a03670d /malloc/tst-memalign.c
parentc6e4925d4069d38843c02994ffd284e8c87c8929 (diff)
downloadglibc-a56ee40b176d0a3f47f2a7eb75208f2e3763c9fd.tar.gz
glibc-a56ee40b176d0a3f47f2a7eb75208f2e3763c9fd.tar.xz
glibc-a56ee40b176d0a3f47f2a7eb75208f2e3763c9fd.zip
malloc: Fix for infinite loop in memalign/posix_memalign.
A very large alignment argument passed to mealign/posix_memalign
causes _int_memalign to enter an infinite loop. Limit the maximum
alignment value to the maximum representable power of two to
prevent this from happening.

Changelog:

2013-10-30  Will Newton  <will.newton@linaro.org>

	[BZ #16038]
	* malloc/hooks.c (memalign_check): Limit alignment to the
	maximum representable power of two.
	* malloc/malloc.c (__libc_memalign): Likewise.
	* malloc/tst-memalign.c (do_test): Add test for very
	large alignment values.
	* malloc/tst-posix_memalign.c (do_test): Likewise.
Diffstat (limited to 'malloc/tst-memalign.c')
-rw-r--r--malloc/tst-memalign.c15
1 files changed, 15 insertions, 0 deletions
diff --git a/malloc/tst-memalign.c b/malloc/tst-memalign.c
index 1c59752483..cf48e7ed1f 100644
--- a/malloc/tst-memalign.c
+++ b/malloc/tst-memalign.c
@@ -70,6 +70,21 @@ do_test (void)
 
   free (p);
 
+  errno = 0;
+
+  /* Test to expose integer overflow in malloc internals from BZ #16038.  */
+  p = memalign (-1, pagesize);
+
+  save = errno;
+
+  if (p != NULL)
+    merror ("memalign (-1, pagesize) succeeded.");
+
+  if (p == NULL && save != EINVAL)
+    merror ("memalign (-1, pagesize) errno is not set correctly");
+
+  free (p);
+
   /* A zero-sized allocation should succeed with glibc, returning a
      non-NULL value.  */
   p = memalign (sizeof (void *), 0);