tst-valloc.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* Test for valloc.
  2. Copyright (C) 2013-2019 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. <http://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. static int errors = 0;
  21. static void
  22. merror (const char *msg)
  23. {
  24. ++errors;
  25. printf ("Error: %s\n", msg);
  26. }
  27. static int
  28. do_test (void)
  29. {
  30. void *p;
  31. unsigned long pagesize = getpagesize ();
  32. unsigned long ptrval;
  33. int save;
  34. errno = 0;
  35. /* An attempt to allocate a huge value should return NULL and set
  36. errno to ENOMEM. */
  37. p = valloc (-1);
  38. save = errno;
  39. if (p != NULL)
  40. merror ("valloc (-1) succeeded.");
  41. if (p == NULL && save != ENOMEM)
  42. merror ("valloc (-1) errno is not set correctly");
  43. free (p);
  44. errno = 0;
  45. /* Test to expose integer overflow in malloc internals from BZ #15856. */
  46. p = valloc (-pagesize);
  47. save = errno;
  48. if (p != NULL)
  49. merror ("valloc (-pagesize) succeeded.");
  50. if (p == NULL && save != ENOMEM)
  51. merror ("valloc (-pagesize) errno is not set correctly");
  52. free (p);
  53. /* A zero-sized allocation should succeed with glibc, returning a
  54. non-NULL value. */
  55. p = valloc (0);
  56. if (p == NULL)
  57. merror ("valloc (0) failed.");
  58. free (p);
  59. /* Check the alignment of the returned pointer is correct. */
  60. p = valloc (32);
  61. if (p == NULL)
  62. merror ("valloc (32) failed.");
  63. ptrval = (unsigned long) p;
  64. if ((ptrval & (pagesize - 1)) != 0)
  65. merror ("returned pointer is not page aligned.");
  66. free (p);
  67. return errors != 0;
  68. }
  69. #define TEST_FUNCTION do_test ()
  70. #include "../test-skeleton.c"