tst-mcheck.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 !defined(__UCLIBC__) || defined(__MALLOC_GLIBC_COMPAT__)
  44. if (p == NULL)
  45. #else
  46. if (p != NULL)
  47. #endif
  48. merror ("malloc (0) failed.");
  49. p = realloc (p, 0);
  50. if (p != NULL)
  51. merror ("realloc (p, 0) failed.");
  52. q = malloc (256);
  53. if (q == NULL)
  54. merror ("malloc (256) failed.");
  55. p = malloc (512);
  56. if (p == NULL)
  57. merror ("malloc (512) failed.");
  58. if (realloc (p, -256) != NULL)
  59. merror ("realloc (p, -256) succeeded.");
  60. else if (errno != ENOMEM)
  61. merror ("errno is not set correctly.");
  62. free (p);
  63. p = malloc (512);
  64. if (p == NULL)
  65. merror ("malloc (512) failed.");
  66. if (realloc (p, -1) != NULL)
  67. merror ("realloc (p, -1) succeeded.");
  68. else if (errno != ENOMEM)
  69. merror ("errno is not set correctly.");
  70. free (p);
  71. free (q);
  72. return errors != 0;
  73. }