xvasprintf.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* vasprintf and asprintf with out-of-memory checking.
  2. Copyright (C) 1999, 2002-2004, 2006-2016 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. errno = EOVERFLOW;
  46. return NULL;
  47. }
  48. /* Allocate and fill the result string. */
  49. result = XNMALLOC (totalsize + 1, char);
  50. p = result;
  51. for (i = argcount; i > 0; i--)
  52. {
  53. const char *next = va_arg (args, const char *);
  54. size_t len = strlen (next);
  55. memcpy (p, next, len);
  56. p += len;
  57. }
  58. *p = '\0';
  59. return result;
  60. }
  61. char *
  62. xvasprintf (const char *format, va_list args)
  63. {
  64. char *result;
  65. /* Recognize the special case format = "%s...%s". It is a frequently used
  66. idiom for string concatenation and needs to be fast. We don't want to
  67. have a separate function xstrcat() for this purpose. */
  68. {
  69. size_t argcount = 0;
  70. const char *f;
  71. for (f = format;;)
  72. {
  73. if (*f == '\0')
  74. /* Recognized the special case of string concatenation. */
  75. return xstrcat (argcount, args);
  76. if (*f != '%')
  77. break;
  78. f++;
  79. if (*f != 's')
  80. break;
  81. f++;
  82. argcount++;
  83. }
  84. }
  85. if (vasprintf (&result, format, args) < 0)
  86. {
  87. if (errno == ENOMEM)
  88. xalloc_die ();
  89. return NULL;
  90. }
  91. return result;
  92. }