]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - malloc/tst-pvalloc.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / malloc / tst-pvalloc.c
index 1126672bd48641dec5c126bfe68dca971a3cb85f..b78ef6aa76990aa2ee13541d3649458a68d0ffcd 100644 (file)
@@ -1,4 +1,5 @@
-/* Copyright (C) 2013 Free Software Foundation, Inc.
+/* Test for pvalloc.
+   Copyright (C) 2013-2019 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
 
    You should have received a copy of the GNU Lesser General Public
    License along with the GNU C Library; if not, see
-   <http://www.gnu.org/licenses/>.  */
+   <https://www.gnu.org/licenses/>.  */
 
 #include <errno.h>
 #include <malloc.h>
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
+#include <libc-diag.h>
 
 static int errors = 0;
 
@@ -34,13 +36,24 @@ static int
 do_test (void)
 {
   void *p;
-  unsigned long pagesize = getpagesize();
+  unsigned long pagesize = getpagesize ();
   unsigned long ptrval;
   int save;
 
   errno = 0;
 
+  DIAG_PUSH_NEEDS_COMMENT;
+#if __GNUC_PREREQ (7, 0)
+  /* GCC 7 warns about too-large allocations; here we want to test
+     that they fail.  */
+  DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
+#endif
+  /* An attempt to allocate a huge value should return NULL and set
+     errno to ENOMEM.  */
   p = pvalloc (-1);
+#if __GNUC_PREREQ (7, 0)
+  DIAG_POP_NEEDS_COMMENT;
+#endif
 
   save = errno;
 
@@ -50,8 +63,11 @@ do_test (void)
   if (p == NULL && save != ENOMEM)
     merror ("pvalloc (-1) errno is not set correctly");
 
+  free (p);
+
   errno = 0;
 
+  /* Test to expose integer overflow in malloc internals from BZ #15855.  */
   p = pvalloc (-pagesize);
 
   save = errno;
@@ -62,6 +78,10 @@ do_test (void)
   if (p == NULL && save != ENOMEM)
     merror ("pvalloc (-pagesize) errno is not set correctly");
 
+  free (p);
+
+  /* A zero-sized allocation should succeed with glibc, returning a
+     non-NULL value.  */
   p = pvalloc (0);
 
   if (p == NULL)
@@ -69,15 +89,13 @@ do_test (void)
 
   free (p);
 
+  /* Check the alignment of the returned pointer is correct.  */
   p = pvalloc (32);
 
   if (p == NULL)
     merror ("pvalloc (32) failed.");
 
-  ptrval = (unsigned long)p;
-
-  if (p == NULL)
-    merror ("pvalloc (32) failed.");
+  ptrval = (unsigned long) p;
 
   if ((ptrval & (pagesize - 1)) != 0)
     merror ("returned pointer is not page aligned.");