xvasprintf.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* vasprintf and asprintf with out-of-memory checking.
  2. Copyright (C) 1999, 2002-2004, 2006-2013 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program 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
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. #include <config.h>
  14. /* Specification. */
  15. #include "xvasprintf.h"
  16. #include <errno.h>
  17. #include <limits.h>
  18. #include <string.h>
  19. #include <stdio.h>
  20. #include "xalloc.h"
  21. /* Checked size_t computations. */
  22. #include "xsize.h"
  23. static char *
  24. xstrcat (size_t argcount, va_list args)
  25. {
  26. char *result;
  27. va_list ap;
  28. size_t totalsize;
  29. size_t i;
  30. char *p;
  31. /* Determine the total size. */
  32. totalsize = 0;
  33. va_copy (ap, args);
  34. for (i = argcount; i > 0; i--)
  35. {
  36. const char *next = va_arg (ap, const char *);
  37. totalsize = xsum (totalsize, strlen (next));
  38. }
  39. va_end (ap);
  40. /* Test for overflow in the summing pass above or in (totalsize + 1) below.
  41. Also, don't return a string longer than INT_MAX, for consistency with
  42. vasprintf(). */
  43. if (totalsize == SIZE_MAX || totalsize > INT_MAX)
  44. {
  45. #if (defined _MSC_VER) && (_MSC_VER < 1800)
  46. #else
  47. errno = EOVERFLOW;
  48. #endif
  49. return NULL;
  50. }
  51. /* Allocate and fill the result string. */
  52. result = XNMALLOC (totalsize + 1, char);
  53. p = result;
  54. for (i = argcount; i > 0; i--)
  55. {
  56. const char *next = va_arg (args, const char *);
  57. size_t len = strlen (next);
  58. memcpy (p, next, len);
  59. p += len;
  60. }
  61. *p = '\0';
  62. return result;
  63. }
  64. #if defined(_MSC_VER)
  65. int vasprintf(char **resultp, const char *format, va_list args);
  66. #endif
  67. char *
  68. xvasprintf (const char *format, va_list args)
  69. {
  70. char *result;
  71. /* Recognize the special case format = "%s...%s". It is a frequently used
  72. idiom for string concatenation and needs to be fast. We don't want to
  73. have a separate function xstrcat() for this purpose. */
  74. {
  75. size_t argcount = 0;
  76. const char *f;
  77. for (f = format;;)
  78. {
  79. if (*f == '\0')
  80. /* Recognized the special case of string concatenation. */
  81. return xstrcat (argcount, args);
  82. if (*f != '%')
  83. break;
  84. f++;
  85. if (*f != 's')
  86. break;
  87. f++;
  88. argcount++;
  89. }
  90. }
  91. if (vasprintf (&result, format, args) < 0)
  92. {
  93. if (errno == ENOMEM)
  94. xalloc_die ();
  95. return NULL;
  96. }
  97. return result;
  98. }