blob: 0b5700a0af1bf0991e102eaad907be1f18edc937 [file] [log] [blame]
George Burgess IVb97049c2017-07-24 15:05:05 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#ifndef _STDIO_H_
30#error "Never include this file directly; instead, include <stdio.h>"
31#endif
32
33char* __fgets_chk(char*, int, FILE*, size_t) __INTRODUCED_IN(17);
Elliott Hughesec6850d2017-08-01 08:28:46 -070034size_t __fread_chk(void*, size_t, size_t, FILE*, size_t) __INTRODUCED_IN(24);
35size_t __fwrite_chk(const void*, size_t, size_t, FILE*, size_t) __INTRODUCED_IN(24);
George Burgess IVb97049c2017-07-24 15:05:05 -070036
37#if defined(__BIONIC_FORTIFY) && !defined(__BIONIC_NO_STDIO_FORTIFY)
38
39#if __ANDROID_API__ >= __ANDROID_API_J_MR1__
40__BIONIC_FORTIFY_INLINE __printflike(3, 0)
George Burgess IV23dbf822017-07-31 21:23:34 -070041int vsnprintf(char* const __pass_object_size dest, size_t size, const char* format, va_list ap)
42 __overloadable {
George Burgess IVb97049c2017-07-24 15:05:05 -070043 return __builtin___vsnprintf_chk(dest, size, 0, __bos(dest), format, ap);
44}
45
46__BIONIC_FORTIFY_INLINE __printflike(2, 0)
George Burgess IV23dbf822017-07-31 21:23:34 -070047int vsprintf(char* const __pass_object_size dest, const char* format, va_list ap) __overloadable {
George Burgess IVb97049c2017-07-24 15:05:05 -070048 return __builtin___vsprintf_chk(dest, 0, __bos(dest), format, ap);
49}
50#endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */
51
George Burgess IVb97049c2017-07-24 15:05:05 -070052#if __ANDROID_API__ >= __ANDROID_API_J_MR1__
53/*
54 * Simple case: `format` can't have format specifiers, so we can just compare
55 * its length to the length of `dest`
56 */
57__BIONIC_ERROR_FUNCTION_VISIBILITY
Elliott Hughesec6850d2017-08-01 08:28:46 -070058int snprintf(char* dest, size_t size, const char* format)
George Burgess IVb97049c2017-07-24 15:05:05 -070059 __overloadable
60 __enable_if(__bos(dest) != __BIONIC_FORTIFY_UNKNOWN_SIZE &&
George Burgess IV23dbf822017-07-31 21:23:34 -070061 __bos(dest) < __builtin_strlen(format),
George Burgess IVb97049c2017-07-24 15:05:05 -070062 "format string will always overflow destination buffer")
63 __errorattr("format string will always overflow destination buffer");
64
Chih-Hung Hsiehf81abef2018-01-25 15:30:27 -080065__BIONIC_FORTIFY_VARIADIC __printflike(3, 4)
George Burgess IV23dbf822017-07-31 21:23:34 -070066int snprintf(char* const __pass_object_size dest, size_t size, const char* format, ...)
67 __overloadable {
George Burgess IVb97049c2017-07-24 15:05:05 -070068 va_list va;
69 va_start(va, format);
70 int result = __builtin___vsnprintf_chk(dest, size, 0, __bos(dest), format, va);
71 va_end(va);
72 return result;
73}
74
75__BIONIC_ERROR_FUNCTION_VISIBILITY
George Burgess IV23dbf822017-07-31 21:23:34 -070076int sprintf(char* dest, const char* format)
77 __overloadable
George Burgess IVb97049c2017-07-24 15:05:05 -070078 __enable_if(__bos(dest) != __BIONIC_FORTIFY_UNKNOWN_SIZE &&
79 __bos(dest) < __builtin_strlen(format),
80 "format string will always overflow destination buffer")
81 __errorattr("format string will always overflow destination buffer");
82
Chih-Hung Hsiehf81abef2018-01-25 15:30:27 -080083__BIONIC_FORTIFY_VARIADIC __printflike(2, 3)
Elliott Hughesec6850d2017-08-01 08:28:46 -070084int sprintf(char* const __pass_object_size dest, const char* format, ...) __overloadable {
George Burgess IVb97049c2017-07-24 15:05:05 -070085 va_list va;
86 va_start(va, format);
87 int result = __builtin___vsprintf_chk(dest, 0, __bos(dest), format, va);
88 va_end(va);
89 return result;
90}
91#endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */
92
93#if __ANDROID_API__ >= __ANDROID_API_N__
94__BIONIC_FORTIFY_INLINE
George Burgess IV23dbf822017-07-31 21:23:34 -070095size_t fread(void* const __pass_object_size0 buf, size_t size, size_t count, FILE* stream)
96 __overloadable
97 __clang_error_if(__unsafe_check_mul_overflow(size, count),
98 "in call to 'fread', size * count overflows")
99 __clang_error_if(__bos(buf) != __BIONIC_FORTIFY_UNKNOWN_SIZE && size * count > __bos(buf),
100 "in call to 'fread', size * count is too large for the given buffer") {
George Burgess IVb97049c2017-07-24 15:05:05 -0700101 size_t bos = __bos0(buf);
102
103 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
104 return __call_bypassing_fortify(fread)(buf, size, count, stream);
105 }
George Burgess IVb97049c2017-07-24 15:05:05 -0700106 return __fread_chk(buf, size, count, stream, bos);
107}
108
George Burgess IVb97049c2017-07-24 15:05:05 -0700109__BIONIC_FORTIFY_INLINE
Elliott Hughesec6850d2017-08-01 08:28:46 -0700110size_t fwrite(const void* const __pass_object_size0 buf, size_t size, size_t count, FILE* stream)
George Burgess IV23dbf822017-07-31 21:23:34 -0700111 __overloadable
112 __clang_error_if(__unsafe_check_mul_overflow(size, count),
113 "in call to 'fwrite', size * count overflows")
114 __clang_error_if(__bos(buf) != __BIONIC_FORTIFY_UNKNOWN_SIZE && size * count > __bos(buf),
115 "in call to 'fwrite', size * count is too large for the given buffer") {
George Burgess IVb97049c2017-07-24 15:05:05 -0700116 size_t bos = __bos0(buf);
117
118 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
119 return __call_bypassing_fortify(fwrite)(buf, size, count, stream);
120 }
121
122 return __fwrite_chk(buf, size, count, stream, bos);
123}
124#endif /* __ANDROID_API__ >= __ANDROID_API_N__ */
125
126#if __ANDROID_API__ >= __ANDROID_API_J_MR1__
George Burgess IVb97049c2017-07-24 15:05:05 -0700127__BIONIC_FORTIFY_INLINE
George Burgess IV23dbf822017-07-31 21:23:34 -0700128char* fgets(char* const __pass_object_size dest, int size, FILE* stream)
129 __overloadable
130 __clang_error_if(size < 0, "in call to 'fgets', size should not be negative")
131 __clang_error_if(size > __bos(dest),
132 "in call to 'fgets', size is larger than the destination buffer") {
George Burgess IVb97049c2017-07-24 15:05:05 -0700133 size_t bos = __bos(dest);
134
135 if (bos == __BIONIC_FORTIFY_UNKNOWN_SIZE) {
136 return __call_bypassing_fortify(fgets)(dest, size, stream);
137 }
138
139 return __fgets_chk(dest, size, stream, bos);
140}
141#endif /* __ANDROID_API__ >= __ANDROID_API_J_MR1__ */
142
George Burgess IVb97049c2017-07-24 15:05:05 -0700143#endif /* defined(__BIONIC_FORTIFY) */