1 /* Copyright (C) 1993-2024 Free Software Foundation, Inc.
2 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.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>.
18 As a special exception, if you link the code in this file with
19 files compiled with a GNU compiler to produce an executable,
20 that does not cause the resulting executable to be covered by
21 the GNU Lesser General Public License. This exception does not
22 however invalidate any other reasons why the executable file
23 might be covered by the GNU Lesser General Public License.
24 This exception applies to code released by its copyright holders
25 in files containing the exception. */
31 #include <printf_buffer.h>
34 __vsprintf_internal (char *string
, size_t maxlen
,
35 const char *format
, va_list args
,
36 unsigned int mode_flags
)
38 struct __printf_buffer buf
;
40 /* When called from fortified sprintf/vsprintf, erase the destination
41 buffer and try to detect overflows. When called from regular
42 sprintf/vsprintf, do not erase the destination buffer, because
43 known user code relies on this behavior (even though its undefined
44 by ISO C), nor try to detect overflows. */
45 if ((mode_flags
& PRINTF_CHK
) != 0)
48 /* In some cases, __sprintf_chk is called with an unknown buffer
49 size (the special value -1). Prevent pointer wraparound in
50 this case and saturate to the end of the address space. */
52 if (__builtin_add_overflow ((uintptr_t) string
, maxlen
, &end
))
54 __printf_buffer_init_end (&buf
, string
, (char *) end
,
55 __printf_buffer_mode_sprintf_chk
);
58 /* Use end of address space. */
59 __printf_buffer_init_end (&buf
, string
, (char *) ~(uintptr_t) 0,
60 __printf_buffer_mode_sprintf
);
62 __printf_buffer (&buf
, format
, args
, mode_flags
);
64 /* Write the NUL terminator if there is room. Do not use the putc
65 operation to avoid overflowing the character write count. */
66 if ((mode_flags
& PRINTF_CHK
) != 0 && buf
.write_ptr
== buf
.write_end
)
68 *buf
.write_ptr
= '\0';
70 return __printf_buffer_done (&buf
);
74 __vsprintf (char *string
, const char *format
, va_list args
)
76 return __vsprintf_internal (string
, -1, format
, args
, 0);
79 ldbl_strong_alias (__vsprintf
, _IO_vsprintf
)
80 ldbl_weak_alias (__vsprintf
, vsprintf
)