tst-pvalloc.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* Test for pvalloc.
  2. Copyright (C) 2013-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <malloc.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #include <libc-diag.h>
  21. #include "tst-malloc-aux.h"
  22. static int errors = 0;
  23. static void
  24. merror (const char *msg)
  25. {
  26. ++errors;
  27. printf ("Error: %s\n", msg);
  28. }
  29. static int
  30. do_test (void)
  31. {
  32. void *p;
  33. unsigned long pagesize = getpagesize ();
  34. unsigned long ptrval;
  35. int save;
  36. errno = 0;
  37. DIAG_PUSH_NEEDS_COMMENT;
  38. #if __GNUC_PREREQ (7, 0)
  39. /* GCC 7 warns about too-large allocations; here we want to test
  40. that they fail. */
  41. DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
  42. #endif
  43. /* An attempt to allocate a huge value should return NULL and set
  44. errno to ENOMEM. */
  45. p = pvalloc (-1);
  46. #if __GNUC_PREREQ (7, 0)
  47. DIAG_POP_NEEDS_COMMENT;
  48. #endif
  49. save = errno;
  50. if (p != NULL)
  51. merror ("pvalloc (-1) succeeded.");
  52. if (p == NULL && save != ENOMEM)
  53. merror ("pvalloc (-1) errno is not set correctly");
  54. free (p);
  55. errno = 0;
  56. /* Test to expose integer overflow in malloc internals from BZ #15855. */
  57. p = pvalloc (-pagesize);
  58. save = errno;
  59. if (p != NULL)
  60. merror ("pvalloc (-pagesize) succeeded.");
  61. if (p == NULL && save != ENOMEM)
  62. merror ("pvalloc (-pagesize) errno is not set correctly");
  63. free (p);
  64. /* A zero-sized allocation should succeed with glibc, returning a
  65. non-NULL value. */
  66. p = pvalloc (0);
  67. if (p == NULL)
  68. merror ("pvalloc (0) failed.");
  69. free (p);
  70. /* Check the alignment of the returned pointer is correct. */
  71. p = pvalloc (32);
  72. if (p == NULL)
  73. merror ("pvalloc (32) failed.");
  74. ptrval = (unsigned long) p;
  75. if ((ptrval & (pagesize - 1)) != 0)
  76. merror ("returned pointer is not page aligned.");
  77. free (p);
  78. return errors != 0;
  79. }
  80. #define TEST_FUNCTION do_test ()
  81. #include "../test-skeleton.c"