blob: f9faeba87e36e4e3dd1687d6e9c8ccd4a27778c5 [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
Elliott Hughes655e4302023-06-16 12:39:33 -070033char* _Nullable __fgets_chk(char* _Nonnull, int, FILE* _Nonnull, size_t);
Dan Albert02ce4012024-10-25 19:13:49 +000034
35#if __BIONIC_AVAILABILITY_GUARD(24)
zijunzhaoacd090d2023-06-02 19:46:52 +000036size_t __fread_chk(void* _Nonnull, size_t, size_t, FILE* _Nonnull, size_t) __INTRODUCED_IN(24);
37size_t __fwrite_chk(const void* _Nonnull, size_t, size_t, FILE* _Nonnull, size_t) __INTRODUCED_IN(24);
Dan Albert02ce4012024-10-25 19:13:49 +000038#endif /* __BIONIC_AVAILABILITY_GUARD(24) */
39
George Burgess IVb97049c2017-07-24 15:05:05 -070040
41#if defined(__BIONIC_FORTIFY) && !defined(__BIONIC_NO_STDIO_FORTIFY)
42
Elliott Hughesf4ace9d2023-02-23 17:38:37 +000043#if __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
George Burgess IV36926f42019-09-15 16:57:00 -070044/* No diag -- clang diagnoses misuses of this on its own. */
George Burgess IVb97049c2017-07-24 15:05:05 -070045__BIONIC_FORTIFY_INLINE __printflike(3, 0)
zijunzhaoacd090d2023-06-02 19:46:52 +000046int vsnprintf(char* const __BIONIC_COMPLICATED_NULLNESS __pass_object_size dest, size_t size, const char* _Nonnull format, va_list ap)
Pirama Arumuga Nainardb71def2022-01-06 15:46:55 -080047 __diagnose_as_builtin(__builtin_vsnprintf, 1, 2, 3, 4)
George Burgess IV23dbf822017-07-31 21:23:34 -070048 __overloadable {
George Burgess IVb97049c2017-07-24 15:05:05 -070049 return __builtin___vsnprintf_chk(dest, size, 0, __bos(dest), format, ap);
50}
51
52__BIONIC_FORTIFY_INLINE __printflike(2, 0)
zijunzhaoacd090d2023-06-02 19:46:52 +000053int vsprintf(char* const __BIONIC_COMPLICATED_NULLNESS __pass_object_size dest, const char* _Nonnull format, va_list ap) __overloadable {
George Burgess IVb97049c2017-07-24 15:05:05 -070054 return __builtin___vsprintf_chk(dest, 0, __bos(dest), format, ap);
55}
George Burgess IV8a0cdb12019-10-21 13:27:57 -070056#endif
George Burgess IVb97049c2017-07-24 15:05:05 -070057
George Burgess IV113d6fa2019-09-18 17:06:14 -070058__BIONIC_ERROR_FUNCTION_VISIBILITY
zijunzhaoacd090d2023-06-02 19:46:52 +000059int sprintf(char* __BIONIC_COMPLICATED_NULLNESS dest, const char* _Nonnull format)
George Burgess IV113d6fa2019-09-18 17:06:14 -070060 __overloadable
61 __enable_if(__bos_unevaluated_lt(__bos(dest), __builtin_strlen(format)),
62 "format string will always overflow destination buffer")
63 __errorattr("format string will always overflow destination buffer");
64
Elliott Hughesf4ace9d2023-02-23 17:38:37 +000065#if __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
George Burgess IV113d6fa2019-09-18 17:06:14 -070066__BIONIC_FORTIFY_VARIADIC __printflike(2, 3)
zijunzhaoacd090d2023-06-02 19:46:52 +000067int sprintf(char* const __BIONIC_COMPLICATED_NULLNESS __pass_object_size dest, const char* _Nonnull format, ...) __overloadable {
George Burgess IV113d6fa2019-09-18 17:06:14 -070068 va_list va;
69 va_start(va, format);
70 int result = __builtin___vsprintf_chk(dest, 0, __bos(dest), format, va);
71 va_end(va);
72 return result;
73}
74
George Burgess IV36926f42019-09-15 16:57:00 -070075/* No diag -- clang diagnoses misuses of this on its own. */
Chih-Hung Hsiehf81abef2018-01-25 15:30:27 -080076__BIONIC_FORTIFY_VARIADIC __printflike(3, 4)
zijunzhaoacd090d2023-06-02 19:46:52 +000077int snprintf(char* const __BIONIC_COMPLICATED_NULLNESS __pass_object_size dest, size_t size, const char* _Nonnull format, ...)
Pirama Arumuga Nainardb71def2022-01-06 15:46:55 -080078 __diagnose_as_builtin(__builtin_snprintf, 1, 2, 3)
George Burgess IV23dbf822017-07-31 21:23:34 -070079 __overloadable {
George Burgess IVb97049c2017-07-24 15:05:05 -070080 va_list va;
81 va_start(va, format);
82 int result = __builtin___vsnprintf_chk(dest, size, 0, __bos(dest), format, va);
83 va_end(va);
84 return result;
85}
George Burgess IV8a0cdb12019-10-21 13:27:57 -070086#endif
George Burgess IVb97049c2017-07-24 15:05:05 -070087
George Burgess IV113d6fa2019-09-18 17:06:14 -070088#define __bos_trivially_ge_mul(bos_val, size, count) \
George Burgess IVa1a09b22019-05-13 17:16:20 -070089 __bos_dynamic_check_impl_and(bos_val, >=, (size) * (count), \
90 !__unsafe_check_mul_overflow(size, count))
91
George Burgess IVb97049c2017-07-24 15:05:05 -070092__BIONIC_FORTIFY_INLINE
zijunzhaoacd090d2023-06-02 19:46:52 +000093size_t fread(void* const _Nonnull __pass_object_size0 buf, size_t size, size_t count, FILE* _Nonnull stream)
George Burgess IV23dbf822017-07-31 21:23:34 -070094 __overloadable
95 __clang_error_if(__unsafe_check_mul_overflow(size, count),
96 "in call to 'fread', size * count overflows")
George Burgess IV5273dc52019-05-09 13:46:57 -070097 __clang_error_if(__bos_unevaluated_lt(__bos0(buf), size * count),
George Burgess IV23dbf822017-07-31 21:23:34 -070098 "in call to 'fread', size * count is too large for the given buffer") {
Elliott Hughes95c6cd72019-12-20 13:26:14 -080099#if __ANDROID_API__ >= 24 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
George Burgess IVb97049c2017-07-24 15:05:05 -0700100 size_t bos = __bos0(buf);
101
George Burgess IV113d6fa2019-09-18 17:06:14 -0700102 if (!__bos_trivially_ge_mul(bos, size, count)) {
103 return __fread_chk(buf, size, count, stream, bos);
George Burgess IVb97049c2017-07-24 15:05:05 -0700104 }
George Burgess IV8a0cdb12019-10-21 13:27:57 -0700105#endif
George Burgess IV113d6fa2019-09-18 17:06:14 -0700106 return __call_bypassing_fortify(fread)(buf, size, count, stream);
George Burgess IVb97049c2017-07-24 15:05:05 -0700107}
108
George Burgess IVb97049c2017-07-24 15:05:05 -0700109__BIONIC_FORTIFY_INLINE
zijunzhaoacd090d2023-06-02 19:46:52 +0000110size_t fwrite(const void* const _Nonnull __pass_object_size0 buf, size_t size, size_t count, FILE* _Nonnull 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")
George Burgess IV5273dc52019-05-09 13:46:57 -0700114 __clang_error_if(__bos_unevaluated_lt(__bos0(buf), size * count),
George Burgess IV23dbf822017-07-31 21:23:34 -0700115 "in call to 'fwrite', size * count is too large for the given buffer") {
Elliott Hughes95c6cd72019-12-20 13:26:14 -0800116#if __ANDROID_API__ >= 24 && __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
George Burgess IVb97049c2017-07-24 15:05:05 -0700117 size_t bos = __bos0(buf);
118
George Burgess IV113d6fa2019-09-18 17:06:14 -0700119 if (!__bos_trivially_ge_mul(bos, size, count)) {
120 return __fwrite_chk(buf, size, count, stream, bos);
George Burgess IVb97049c2017-07-24 15:05:05 -0700121 }
George Burgess IV8a0cdb12019-10-21 13:27:57 -0700122#endif
George Burgess IV113d6fa2019-09-18 17:06:14 -0700123 return __call_bypassing_fortify(fwrite)(buf, size, count, stream);
124}
125#undef __bos_trivially_ge_mul
George Burgess IVb97049c2017-07-24 15:05:05 -0700126
George Burgess IVb97049c2017-07-24 15:05:05 -0700127__BIONIC_FORTIFY_INLINE
zijunzhaoacd090d2023-06-02 19:46:52 +0000128char* _Nullable fgets(char* const _Nonnull __pass_object_size dest, int size, FILE* _Nonnull stream)
George Burgess IV23dbf822017-07-31 21:23:34 -0700129 __overloadable
130 __clang_error_if(size < 0, "in call to 'fgets', size should not be negative")
George Burgess IV5273dc52019-05-09 13:46:57 -0700131 __clang_error_if(__bos_unevaluated_lt(__bos(dest), size),
George Burgess IV23dbf822017-07-31 21:23:34 -0700132 "in call to 'fgets', size is larger than the destination buffer") {
Elliott Hughesf4ace9d2023-02-23 17:38:37 +0000133#if __BIONIC_FORTIFY_RUNTIME_CHECKS_ENABLED
George Burgess IVb97049c2017-07-24 15:05:05 -0700134 size_t bos = __bos(dest);
135
George Burgess IV113d6fa2019-09-18 17:06:14 -0700136 if (!__bos_dynamic_check_impl_and(bos, >=, (size_t)size, size >= 0)) {
137 return __fgets_chk(dest, size, stream, bos);
George Burgess IVb97049c2017-07-24 15:05:05 -0700138 }
George Burgess IV8a0cdb12019-10-21 13:27:57 -0700139#endif
George Burgess IV113d6fa2019-09-18 17:06:14 -0700140 return __call_bypassing_fortify(fgets)(dest, size, stream);
141}
George Burgess IVb97049c2017-07-24 15:05:05 -0700142
George Burgess IVb97049c2017-07-24 15:05:05 -0700143#endif /* defined(__BIONIC_FORTIFY) */