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