tst-mcheck.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* Copyright (C) 2005 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Jakub Jelinek <jakub@redhat.com>, 2005.
  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 <stdio.h>
  17. #include <stdlib.h>
  18. #include <features.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. int
  27. main (void)
  28. {
  29. void *p, *q;
  30. errno = 0;
  31. p = malloc (-1);
  32. if (p != NULL)
  33. merror ("malloc (-1) succeeded.");
  34. else if (errno != ENOMEM)
  35. merror ("errno is not set correctly.");
  36. p = malloc (10);
  37. if (p == NULL)
  38. merror ("malloc (10) failed.");
  39. p = realloc (p, 0);
  40. if (p != NULL)
  41. merror ("realloc (p, 0) failed.");
  42. p = malloc (0);
  43. if (p == NULL)
  44. merror ("malloc (0) failed.");
  45. p = realloc (p, 0);
  46. if (p != NULL)
  47. merror ("realloc (p, 0) failed.");
  48. q = malloc (256);
  49. if (q == NULL)
  50. merror ("malloc (256) failed.");
  51. p = malloc (512);
  52. if (p == NULL)
  53. merror ("malloc (512) failed.");
  54. if (realloc (p, -256) != NULL)
  55. merror ("realloc (p, -256) succeeded.");
  56. else if (errno != ENOMEM)
  57. merror ("errno is not set correctly.");
  58. free (p);
  59. p = malloc (512);
  60. if (p == NULL)
  61. merror ("malloc (512) failed.");
  62. if (realloc (p, -1) != NULL)
  63. merror ("realloc (p, -1) succeeded.");
  64. else if (errno != ENOMEM)
  65. merror ("errno is not set correctly.");
  66. free (p);
  67. free (q);
  68. return errors != 0;
  69. }