]>
Commit | Line | Data |
---|---|---|
6d7e8eda | 1 | /* Copyright (C) 1993-2023 Free Software Foundation, Inc. |
41bdb6e2 | 2 | This file is part of the GNU C Library. |
40a55d20 | 3 | |
41bdb6e2 AJ |
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. | |
40a55d20 | 8 | |
41bdb6e2 AJ |
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 | |
40a55d20 | 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
41bdb6e2 AJ |
12 | Lesser General Public License for more details. |
13 | ||
14 | You should have received a copy of the GNU Lesser General Public | |
59ba27a6 | 15 | License along with the GNU C Library; if not, see |
5a82c748 | 16 | <https://www.gnu.org/licenses/>. |
41bdb6e2 AJ |
17 | |
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. */ | |
96aa2d94 RM |
26 | |
27 | #include "libioP.h" | |
96aa2d94 | 28 | |
fb9bd841 FW |
29 | #include <printf.h> |
30 | #include <stdint.h> | |
31 | #include <printf_buffer.h> | |
4e2f43f8 | 32 | |
96aa2d94 | 33 | int |
4e2f43f8 ZW |
34 | __vsprintf_internal (char *string, size_t maxlen, |
35 | const char *format, va_list args, | |
698fb75b | 36 | unsigned int mode_flags) |
96aa2d94 | 37 | { |
fb9bd841 | 38 | struct __printf_buffer buf; |
edf5b2d7 | 39 | |
2d9837c1 GG |
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) | |
46 | { | |
2d9837c1 | 47 | string[0] = '\0'; |
fb9bd841 FW |
48 | __printf_buffer_init (&buf, string, maxlen, |
49 | __printf_buffer_mode_sprintf_chk); | |
2d9837c1 GG |
50 | } |
51 | else | |
fb9bd841 FW |
52 | { |
53 | __printf_buffer_init (&buf, string, 0, __printf_buffer_mode_sprintf); | |
54 | buf.write_end = (char *) ~(uintptr_t) 0; /* End of address space. */ | |
55 | } | |
56 | ||
57 | __printf_buffer (&buf, format, args, mode_flags); | |
4e2f43f8 | 58 | |
fb9bd841 FW |
59 | /* Write the NUL terminator if there is room. Do not use the putc |
60 | operation to avoid overflowing the character write count. */ | |
61 | if ((mode_flags & PRINTF_CHK) != 0 && buf.write_ptr == buf.write_end) | |
62 | __chk_fail (); | |
63 | *buf.write_ptr = '\0'; | |
4e2f43f8 | 64 | |
fb9bd841 | 65 | return __printf_buffer_done (&buf); |
96aa2d94 RM |
66 | } |
67 | ||
698fb75b ZW |
68 | int |
69 | __vsprintf (char *string, const char *format, va_list args) | |
70 | { | |
4e2f43f8 | 71 | return __vsprintf_internal (string, -1, format, args, 0); |
698fb75b ZW |
72 | } |
73 | ||
74 | ldbl_strong_alias (__vsprintf, _IO_vsprintf) | |
75 | ldbl_weak_alias (__vsprintf, vsprintf) |