]> git.ipfire.org Git - thirdparty/glibc.git/blame - libio/iovsprintf.c
benchtests: Add random() benchmark
[thirdparty/glibc.git] / libio / iovsprintf.c
CommitLineData
dff8da6b 1/* Copyright (C) 1993-2024 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 33int
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';
0d50f477
FW
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. */
51 uintptr_t end;
52 if (__builtin_add_overflow ((uintptr_t) string, maxlen, &end))
53 end = -1;
54 __printf_buffer_init_end (&buf, string, (char *) end,
fb9bd841 55 __printf_buffer_mode_sprintf_chk);
2d9837c1
GG
56 }
57 else
0d50f477
FW
58 /* Use end of address space. */
59 __printf_buffer_init_end (&buf, string, (char *) ~(uintptr_t) 0,
60 __printf_buffer_mode_sprintf);
fb9bd841
FW
61
62 __printf_buffer (&buf, format, args, mode_flags);
4e2f43f8 63
fb9bd841
FW
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)
67 __chk_fail ();
68 *buf.write_ptr = '\0';
4e2f43f8 69
fb9bd841 70 return __printf_buffer_done (&buf);
96aa2d94
RM
71}
72
698fb75b
ZW
73int
74__vsprintf (char *string, const char *format, va_list args)
75{
4e2f43f8 76 return __vsprintf_internal (string, -1, format, args, 0);
698fb75b
ZW
77}
78
79ldbl_strong_alias (__vsprintf, _IO_vsprintf)
80ldbl_weak_alias (__vsprintf, vsprintf)