tst-malloc.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* Copyright (C) 1999 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 <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;
  30. int save;
  31. errno = 0;
  32. p = malloc (-1);
  33. save = errno;
  34. if (p != NULL)
  35. merror ("malloc (-1) succeeded.");
  36. if (p == NULL && save != ENOMEM)
  37. merror ("errno is not set correctly");
  38. p = malloc (10);
  39. if (p == NULL)
  40. merror ("malloc (10) failed.");
  41. /* realloc (p, 0) == free (p). */
  42. p = realloc (p, 0);
  43. if (p != NULL)
  44. merror ("realloc (p, 0) failed.");
  45. p = malloc (0);
  46. #if !defined(__UCLIBC__) || defined(__MALLOC_GLIBC_COMPAT__)
  47. if (p == NULL)
  48. #else
  49. if (p != NULL)
  50. #endif
  51. merror ("malloc (0) failed.");
  52. p = realloc (p, 0);
  53. if (p != NULL)
  54. merror ("realloc (p, 0) failed.");
  55. return errors != 0;
  56. }