iovsprintf.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /* Copyright (C) 1993-2019 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. <http://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 "libioP.h"
  23. #include "strfile.h"
  24. static int __THROW
  25. _IO_str_chk_overflow (FILE *fp, int c)
  26. {
  27. /* If we get here, the user-supplied buffer would be overrun by
  28. further output. */
  29. __chk_fail ();
  30. }
  31. static const struct _IO_jump_t _IO_str_chk_jumps libio_vtable =
  32. {
  33. JUMP_INIT_DUMMY,
  34. JUMP_INIT(finish, _IO_str_finish),
  35. JUMP_INIT(overflow, _IO_str_chk_overflow),
  36. JUMP_INIT(underflow, _IO_str_underflow),
  37. JUMP_INIT(uflow, _IO_default_uflow),
  38. JUMP_INIT(pbackfail, _IO_str_pbackfail),
  39. JUMP_INIT(xsputn, _IO_default_xsputn),
  40. JUMP_INIT(xsgetn, _IO_default_xsgetn),
  41. JUMP_INIT(seekoff, _IO_str_seekoff),
  42. JUMP_INIT(seekpos, _IO_default_seekpos),
  43. JUMP_INIT(setbuf, _IO_default_setbuf),
  44. JUMP_INIT(sync, _IO_default_sync),
  45. JUMP_INIT(doallocate, _IO_default_doallocate),
  46. JUMP_INIT(read, _IO_default_read),
  47. JUMP_INIT(write, _IO_default_write),
  48. JUMP_INIT(seek, _IO_default_seek),
  49. JUMP_INIT(close, _IO_default_close),
  50. JUMP_INIT(stat, _IO_default_stat),
  51. JUMP_INIT(showmanyc, _IO_default_showmanyc),
  52. JUMP_INIT(imbue, _IO_default_imbue)
  53. };
  54. /* This function is called by regular vsprintf with maxlen set to -1,
  55. and by vsprintf_chk with maxlen set to the size of the output
  56. string. In the former case, _IO_str_chk_overflow will never be
  57. called; in the latter case it will crash the program if the buffer
  58. overflows. */
  59. int
  60. __vsprintf_internal (char *string, size_t maxlen,
  61. const char *format, va_list args,
  62. unsigned int mode_flags)
  63. {
  64. _IO_strfile sf;
  65. int ret;
  66. #ifdef _IO_MTSAFE_IO
  67. sf._sbf._f._lock = NULL;
  68. #endif
  69. _IO_no_init (&sf._sbf._f, _IO_USER_LOCK, -1, NULL, NULL);
  70. /* When called from fortified sprintf/vsprintf, erase the destination
  71. buffer and try to detect overflows. When called from regular
  72. sprintf/vsprintf, do not erase the destination buffer, because
  73. known user code relies on this behavior (even though its undefined
  74. by ISO C), nor try to detect overflows. */
  75. if ((mode_flags & PRINTF_CHK) != 0)
  76. {
  77. _IO_JUMPS (&sf._sbf) = &_IO_str_chk_jumps;
  78. string[0] = '\0';
  79. }
  80. else
  81. _IO_JUMPS (&sf._sbf) = &_IO_str_jumps;
  82. _IO_str_init_static_internal (&sf, string,
  83. (maxlen == -1) ? -1 : maxlen - 1,
  84. string);
  85. ret = __vfprintf_internal (&sf._sbf._f, format, args, mode_flags);
  86. *sf._sbf._f._IO_write_ptr = '\0';
  87. return ret;
  88. }
  89. int
  90. __vsprintf (char *string, const char *format, va_list args)
  91. {
  92. return __vsprintf_internal (string, -1, format, args, 0);
  93. }
  94. ldbl_strong_alias (__vsprintf, _IO_vsprintf)
  95. ldbl_weak_alias (__vsprintf, vsprintf)