about summary refs log tree commit diff
diff options
context:
space:
mode:
authorWill Newton <will.newton@linaro.org>2013-10-03 11:21:15 +0100
committerWill Newton <will.newton@linaro.org>2013-10-04 09:14:27 +0100
commit27d0461b7bb00e8863dbb2087ce22bf67f9c1018 (patch)
treefa0e5d67c639ea131e099d03f09f6cf321f7b5bf
parent215c7d43444a64fcb571381ebcd39c7514c7b4ff (diff)
downloadglibc-27d0461b7bb00e8863dbb2087ce22bf67f9c1018.tar.gz
glibc-27d0461b7bb00e8863dbb2087ce22bf67f9c1018.tar.xz
glibc-27d0461b7bb00e8863dbb2087ce22bf67f9c1018.zip
malloc/tst-posix_memalign.c: Tidy up code.
Add some comments and call free on all potentially allocated pointers.

ChangeLog:

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

	* malloc/tst-posix_memalign.c: Add comments.
	(do_test): Add comments and call free on all potentially
	allocated pointers. Add space after cast.
-rw-r--r--ChangeLog4
-rw-r--r--malloc/tst-posix_memalign.c18
2 files changed, 18 insertions, 4 deletions
diff --git a/ChangeLog b/ChangeLog
index 7c268814a0..fab61a02c3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -3,6 +3,10 @@
 	* malloc/Makefile: Add tst-memalign.
 	* malloc/tst-memalign.c: New file.
 
+	* malloc/tst-posix_memalign.c: Add comments.
+	(do_test): Add comments and call free on all potentially
+	allocated pointers. Add space after cast.
+
 2013-10-04  Alan Modra  <amodra@gmail.com>
 
 	* sysdeps/powerpc/powerpc32/dl-machine.c (__process_machine_rela):
diff --git a/malloc/tst-posix_memalign.c b/malloc/tst-posix_memalign.c
index 9d9d1bfb0c..27c0dd2bd4 100644
--- a/malloc/tst-posix_memalign.c
+++ b/malloc/tst-posix_memalign.c
@@ -1,4 +1,5 @@
-/* Copyright (C) 2013 Free Software Foundation, Inc.
+/* Test for posix_memalign.
+   Copyright (C) 2013 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -35,11 +36,13 @@ do_test (void)
 {
   void *p;
   int ret;
-  unsigned long pagesize = getpagesize();
+  unsigned long pagesize = getpagesize ();
   unsigned long ptrval;
 
   p = NULL;
 
+  /* An attempt to allocate a huge value should return ENOMEM and
+     p should remain NULL.  */
   ret = posix_memalign (&p, sizeof (void *), -1);
 
   if (ret != ENOMEM)
@@ -48,15 +51,22 @@ do_test (void)
   if (ret == ENOMEM && p != NULL)
     merror ("returned an error but pointer was modified");
 
+  free (p);
+
   p = NULL;
 
+  /* Test to expose integer overflow in malloc internals from BZ #15857.  */
   ret = posix_memalign (&p, pagesize, -pagesize);
 
   if (ret != ENOMEM)
     merror ("posix_memalign (&p, pagesize, -pagesize) succeeded.");
 
+  free (p);
+
   p = NULL;
 
+  /* A zero-sized allocation should succeed with glibc, returning zero
+     and setting p to a non-NULL value.  */
   ret = posix_memalign (&p, sizeof (void *), 0);
 
   if (ret != 0 || p == NULL)
@@ -84,9 +94,9 @@ do_test (void)
   if (ret == 0 && p == NULL)
     merror ("returned success but pointer is NULL");
 
-  ptrval = (unsigned long)p;
+  ptrval = (unsigned long) p;
 
-  if (ret == 0 && (ptrval & 0xff))
+  if (ret == 0 && (ptrval & 0xff) != 0)
     merror ("pointer is not aligned to 0x100");
 
   free (p);