tst-posix_memalign.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* Test for posix_memalign.
  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 <stdlib.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #include <libc-diag.h>
  21. static int errors = 0;
  22. static void
  23. merror (const char *msg)
  24. {
  25. ++errors;
  26. printf ("Error: %s\n", msg);
  27. }
  28. static int
  29. do_test (void)
  30. {
  31. void *p;
  32. int ret;
  33. unsigned long pagesize = getpagesize ();
  34. unsigned long ptrval;
  35. p = NULL;
  36. DIAG_PUSH_NEEDS_COMMENT;
  37. #if __GNUC_PREREQ (7, 0)
  38. /* GCC 7 warns about too-large allocations; here we want to test
  39. that they fail. */
  40. DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
  41. #endif
  42. /* An attempt to allocate a huge value should return ENOMEM and
  43. p should remain NULL. */
  44. ret = posix_memalign (&p, sizeof (void *), -1);
  45. #if __GNUC_PREREQ (7, 0)
  46. DIAG_POP_NEEDS_COMMENT;
  47. #endif
  48. if (ret != ENOMEM)
  49. merror ("posix_memalign (&p, sizeof (void *), -1) succeeded.");
  50. if (ret == ENOMEM && p != NULL)
  51. merror ("returned an error but pointer was modified");
  52. free (p);
  53. p = NULL;
  54. /* Test to expose integer overflow in malloc internals from BZ #15857. */
  55. ret = posix_memalign (&p, pagesize, -pagesize);
  56. if (ret != ENOMEM)
  57. merror ("posix_memalign (&p, pagesize, -pagesize) succeeded.");
  58. free (p);
  59. p = NULL;
  60. /* Test to expose integer overflow in malloc internals from BZ #16038. */
  61. ret = posix_memalign (&p, -1, pagesize);
  62. if (ret != EINVAL)
  63. merror ("posix_memalign (&p, -1, pagesize) succeeded.");
  64. free (p);
  65. p = NULL;
  66. /* A zero-sized allocation should succeed with glibc, returning zero
  67. and setting p to a non-NULL value. */
  68. ret = posix_memalign (&p, sizeof (void *), 0);
  69. if (ret != 0 || p == NULL)
  70. merror ("posix_memalign (&p, sizeof (void *), 0) failed.");
  71. free (p);
  72. ret = posix_memalign (&p, 0x300, 10);
  73. if (ret != EINVAL)
  74. merror ("posix_memalign (&p, 0x300, 10) succeeded.");
  75. ret = posix_memalign (&p, 0, 10);
  76. if (ret != EINVAL)
  77. merror ("posix_memalign (&p, 0, 10) succeeded.");
  78. p = NULL;
  79. ret = posix_memalign (&p, 0x100, 10);
  80. if (ret != 0)
  81. merror ("posix_memalign (&p, 0x100, 10) failed.");
  82. if (ret == 0 && p == NULL)
  83. merror ("returned success but pointer is NULL");
  84. ptrval = (unsigned long) p;
  85. if (ret == 0 && (ptrval & 0xff) != 0)
  86. merror ("pointer is not aligned to 0x100");
  87. free (p);
  88. return errors != 0;
  89. }
  90. #define TEST_FUNCTION do_test ()
  91. #include "../test-skeleton.c"