tst-malloc.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* Copyright (C) 1999-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Andreas Jaeger <aj@arthur.rhein-neckar.de>, 1999.
  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 <malloc.h>
  17. #include <stdio.h>
  18. #include <libc-diag.h>
  19. static int errors = 0;
  20. static void
  21. merror (const char *msg)
  22. {
  23. ++errors;
  24. printf ("Error: %s\n", msg);
  25. }
  26. static int
  27. do_test (void)
  28. {
  29. void *p, *q;
  30. int save;
  31. errno = 0;
  32. DIAG_PUSH_NEEDS_COMMENT;
  33. #if __GNUC_PREREQ (7, 0)
  34. /* GCC 7 warns about too-large allocations; here we want to test
  35. that they fail. */
  36. DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
  37. #endif
  38. p = malloc (-1);
  39. DIAG_POP_NEEDS_COMMENT;
  40. save = errno;
  41. if (p != NULL)
  42. merror ("malloc (-1) succeeded.");
  43. if (p == NULL && save != ENOMEM)
  44. merror ("errno is not set correctly");
  45. p = malloc (10);
  46. if (p == NULL)
  47. merror ("malloc (10) failed.");
  48. /* realloc (p, 0) == free (p). */
  49. p = realloc (p, 0);
  50. if (p != NULL)
  51. merror ("realloc (p, 0) failed.");
  52. p = malloc (0);
  53. if (p == NULL)
  54. merror ("malloc (0) failed.");
  55. p = realloc (p, 0);
  56. if (p != NULL)
  57. merror ("realloc (p, 0) failed.");
  58. p = malloc (513 * 1024);
  59. if (p == NULL)
  60. merror ("malloc (513K) failed.");
  61. DIAG_PUSH_NEEDS_COMMENT;
  62. #if __GNUC_PREREQ (7, 0)
  63. /* GCC 7 warns about too-large allocations; here we want to test
  64. that they fail. */
  65. DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
  66. #endif
  67. q = malloc (-512 * 1024);
  68. DIAG_POP_NEEDS_COMMENT;
  69. if (q != NULL)
  70. merror ("malloc (-512K) succeeded.");
  71. free (p);
  72. return errors != 0;
  73. }
  74. #define TEST_FUNCTION do_test ()
  75. #include "../test-skeleton.c"