tst-asprintf-null.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /* Test that asprintf sets the buffer pointer to NULL on failure.
  2. Copyright (C) 2024-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  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. <https://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <stdio.h>
  17. #include <support/check.h>
  18. #include <sys/resource.h>
  19. static int
  20. do_test (void)
  21. {
  22. static const char sentinel[] = "sentinel";
  23. char *buf = (char *) sentinel;
  24. {
  25. /* Avoid -Wformat-overflow warning. */
  26. const char *volatile format = "%2000000000d %2000000000d";
  27. TEST_COMPARE (asprintf (&buf, format, 1, 2), -1);
  28. }
  29. if (errno != ENOMEM)
  30. TEST_COMPARE (errno, EOVERFLOW);
  31. TEST_VERIFY (buf == NULL);
  32. /* Force ENOMEM in the test below. */
  33. struct rlimit rl;
  34. TEST_COMPARE (getrlimit (RLIMIT_AS, &rl), 0);
  35. rl.rlim_cur = 10 * 1024 * 1024;
  36. TEST_COMPARE (setrlimit (RLIMIT_AS, &rl), 0);
  37. buf = (char *) sentinel;
  38. TEST_COMPARE (asprintf (&buf, "%20000000d", 1), -1);
  39. TEST_COMPARE (errno, ENOMEM);
  40. TEST_VERIFY (buf == NULL);
  41. return 0;
  42. }
  43. #include <support/test-driver.c>