vasprintf.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* Copyright (C) 1995-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>.
  14. As a special exception, if you link the code in this file with
  15. files compiled with a GNU compiler to produce an executable,
  16. that does not cause the resulting executable to be covered by
  17. the GNU Lesser General Public License. This exception does not
  18. however invalidate any other reasons why the executable file
  19. might be covered by the GNU Lesser General Public License.
  20. This exception applies to code released by its copyright holders
  21. in files containing the exception. */
  22. #include <array_length.h>
  23. #include <errno.h>
  24. #include <limits.h>
  25. #include <math_ldbl_opt.h>
  26. #include <printf.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <printf_buffer.h>
  31. struct __printf_buffer_asprintf
  32. {
  33. /* base.write_base points either to a heap-allocated buffer, or to
  34. the direct array below. */
  35. struct __printf_buffer base;
  36. /* Initial allocation. 200 should be large enough to copy almost
  37. all asprintf usages with just a single (final, correctly sized)
  38. heap allocation. */
  39. char direct[PRINTF_BUFFER_SIZE_ASPRINTF];
  40. };
  41. void
  42. __printf_buffer_flush_asprintf (struct __printf_buffer_asprintf *buf)
  43. {
  44. size_t current_pos = buf->base.write_ptr - buf->base.write_base;
  45. if (current_pos >= INT_MAX)
  46. {
  47. /* The result is not representable. No need to continue. */
  48. __set_errno (EOVERFLOW);
  49. __printf_buffer_mark_failed (&buf->base);
  50. return;
  51. }
  52. size_t current_size = buf->base.write_end - buf->base.write_base;
  53. /* Implement an exponentiation sizing policy. Keep the size
  54. congruent 8 (mod 16), to account for the footer in glibc
  55. malloc. */
  56. size_t new_size = ALIGN_UP (current_size + current_size / 2, 16) | 8;
  57. char *new_buffer;
  58. if (buf->base.write_base == buf->direct)
  59. {
  60. new_buffer = malloc (new_size);
  61. if (new_buffer == NULL)
  62. {
  63. __printf_buffer_mark_failed (&buf->base);
  64. return;
  65. }
  66. memcpy (new_buffer, buf->direct, current_pos);
  67. }
  68. else
  69. {
  70. new_buffer = realloc (buf->base.write_base, new_size);
  71. if (new_buffer == NULL)
  72. {
  73. __printf_buffer_mark_failed (&buf->base);
  74. return;
  75. }
  76. }
  77. /* Set up the new write area. */
  78. buf->base.write_base = new_buffer;
  79. buf->base.write_ptr = new_buffer + current_pos;
  80. buf->base.write_end = new_buffer + new_size;
  81. }
  82. int
  83. __vasprintf_internal (char **result, const char *format, va_list args,
  84. unsigned int mode_flags)
  85. {
  86. struct __printf_buffer_asprintf buf;
  87. __printf_buffer_init (&buf.base, buf.direct, array_length (buf.direct),
  88. __printf_buffer_mode_asprintf);
  89. __printf_buffer (&buf.base, format, args, mode_flags);
  90. int done = __printf_buffer_done (&buf.base);
  91. if (done < 0)
  92. {
  93. if (buf.base.write_base != buf.direct)
  94. free (buf.base.write_base);
  95. *result = NULL;
  96. return done;
  97. }
  98. /* Transfer to the final buffer. */
  99. size_t size = buf.base.write_ptr - buf.base.write_base;
  100. if (buf.base.write_base == buf.direct)
  101. {
  102. *result = malloc (size + 1);
  103. if (*result == NULL)
  104. return -1;
  105. memcpy (*result, buf.direct, size);
  106. }
  107. else
  108. {
  109. *result = realloc (buf.base.write_base, size + 1);
  110. if (*result == NULL)
  111. {
  112. free (buf.base.write_base);
  113. return -1;
  114. }
  115. }
  116. /* Add NUL termination. */
  117. (*result)[size] = '\0';
  118. return done;
  119. }
  120. int
  121. __vasprintf (char **result_ptr, const char *format, va_list args)
  122. {
  123. return __vasprintf_internal (result_ptr, format, args, 0);
  124. }
  125. ldbl_weak_alias (__vasprintf, vasprintf)