tst-malloc-check.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* Copyright (C) 2005-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #include <errno.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <libc-diag.h>
  18. #include "tst-malloc-aux.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. errno = 0;
  31. DIAG_PUSH_NEEDS_COMMENT;
  32. #if __GNUC_PREREQ (7, 0)
  33. /* GCC 7 warns about too-large allocations; here we want to test
  34. that they fail. */
  35. DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
  36. #endif
  37. p = malloc (-1);
  38. DIAG_POP_NEEDS_COMMENT;
  39. if (p != NULL)
  40. merror ("malloc (-1) succeeded.");
  41. else if (errno != ENOMEM)
  42. merror ("errno is not set correctly.");
  43. p = malloc (10);
  44. if (p == NULL)
  45. merror ("malloc (10) failed.");
  46. p = realloc (p, 0);
  47. if (p != NULL)
  48. merror ("realloc (p, 0) failed.");
  49. p = malloc (0);
  50. if (p == NULL)
  51. merror ("malloc (0) failed.");
  52. p = realloc (p, 0);
  53. if (p != NULL)
  54. merror ("realloc (p, 0) failed.");
  55. q = malloc (256);
  56. if (q == NULL)
  57. merror ("malloc (256) failed.");
  58. p = malloc (512);
  59. if (p == NULL)
  60. merror ("malloc (512) 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. if (realloc (p, -256) != NULL)
  68. merror ("realloc (p, -256) succeeded.");
  69. else if (errno != ENOMEM)
  70. merror ("errno is not set correctly.");
  71. DIAG_POP_NEEDS_COMMENT;
  72. #if __GNUC_PREREQ (12, 0)
  73. /* Ignore a valid warning about using a pointer made indeterminate
  74. by a prior call to realloc(). */
  75. DIAG_IGNORE_NEEDS_COMMENT (12, "-Wuse-after-free");
  76. #endif
  77. free (p);
  78. #if __GNUC_PREREQ (12, 0)
  79. DIAG_POP_NEEDS_COMMENT;
  80. #endif
  81. p = malloc (512);
  82. if (p == NULL)
  83. merror ("malloc (512) failed.");
  84. DIAG_PUSH_NEEDS_COMMENT;
  85. #if __GNUC_PREREQ (7, 0)
  86. /* GCC 7 warns about too-large allocations; here we want to test
  87. that they fail. */
  88. DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
  89. #endif
  90. if (realloc (p, -1) != NULL)
  91. merror ("realloc (p, -1) succeeded.");
  92. else if (errno != ENOMEM)
  93. merror ("errno is not set correctly.");
  94. DIAG_POP_NEEDS_COMMENT;
  95. #if __GNUC_PREREQ (12, 0)
  96. /* Ignore a valid warning about using a pointer made indeterminate
  97. by a prior call to realloc(). */
  98. DIAG_IGNORE_NEEDS_COMMENT (12, "-Wuse-after-free");
  99. #endif
  100. free (p);
  101. #if __GNUC_PREREQ (12, 0)
  102. DIAG_POP_NEEDS_COMMENT;
  103. #endif
  104. free (q);
  105. return errors != 0;
  106. }
  107. #define TEST_FUNCTION do_test ()
  108. #include "../test-skeleton.c"