tst-malloc.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 (p == NULL)
  47. merror ("malloc (0) failed.");
  48. p = realloc (p, 0);
  49. if (p != NULL)
  50. merror ("realloc (p, 0) failed.");
  51. return errors != 0;
  52. }