1/*
  2 * Copyright (c) 2007, 2010, 2023 Apple Inc. All rights reserved.
  3 *
  4 * @APPLE_LICENSE_HEADER_START@
  5 *
  6 * This file contains Original Code and/or Modifications of Original Code
  7 * as defined in and that are subject to the Apple Public Source License
  8 * Version 2.0 (the 'License'). You may not use this file except in
  9 * compliance with the License. Please obtain a copy of the License at
 10 * http://www.opensource.apple.com/apsl/ and read it before using this
 11 * file.
 12 *
 13 * The Original Code and all software distributed under the License are
 14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 18 * Please see the License for the specific language governing rights and
 19 * limitations under the License.
 20 *
 21 * @APPLE_LICENSE_HEADER_END@
 22 */
 23
 24#ifndef _STDIO_H_
 25 #error error "Never use <secure/_stdio.h> directly; include <stdio.h> instead."
 26#endif
 27
 28#ifndef _SECURE__STDIO_H_
 29#define _SECURE__STDIO_H_
 30
 31#include <_bounds.h>
 32#include <secure/_common.h>
 33
 34_LIBC_SINGLE_BY_DEFAULT()
 35
 36#if _USE_FORTIFY_LEVEL > 0
 37
 38extern int __snprintf_chk (char * __restrict _LIBC_COUNT(__maxlen), size_t __maxlen, int, size_t,
 39			  const char * __restrict, ...);
 40extern int __vsnprintf_chk (char * __restrict _LIBC_COUNT(__maxlen), size_t __maxlen, int, size_t,
 41			  const char * __restrict, va_list);
 42
 43extern int __sprintf_chk (char * __restrict _LIBC_UNSAFE_INDEXABLE, int, size_t,
 44			  const char * __restrict, ...);
 45extern int __vsprintf_chk (char * __restrict _LIBC_UNSAFE_INDEXABLE, int, size_t,
 46			  const char * __restrict, va_list);
 47
 48#ifdef __LIBC_STAGED_BOUNDS_SAFETY_ATTRIBUTES
 49
 50/* verify that there are at least __n characters at __str */
 51static inline char *_LIBC_COUNT(__n)
 52__libc_ptrchk_strbuf_chk(char *_LIBC_COUNT(__n) __str, size_t __n) { return __str; }
 53
 54#undef __sprintf_chk_func /* sprintf is unavailable */
 55#undef __vsprintf_chk_func /* vsprintf is unavailable */
 56
 57#define __vsnprintf_chk_func(str, len, flag, format, ap) ({ \
 58	size_t __len = (len); \
 59	__builtin___vsnprintf_chk (__libc_ptrchk_strbuf_chk(str, __len), __len, flag, __darwin_obsz(str), format, ap); \
 60})
 61
 62#define __snprintf_chk_func(str, len, flag, ...) ({ \
 63	size_t __len = (len); \
 64	__builtin___snprintf_chk (__libc_ptrchk_strbuf_chk(str, __len), __len, flag, __darwin_obsz(str), __VA_ARGS__); \
 65})
 66
 67#else
 68
 69#ifndef __has_builtin
 70#define __undef__has_builtin
 71#define __has_builtin(x) defined(__GNUC__)
 72#endif
 73
 74#if __has_builtin(__builtin___snprintf_chk)
 75#define __snprintf_chk_func(str, len, flag, ...) \
 76	__builtin___snprintf_chk (str, len, flag, __darwin_obsz(str), __VA_ARGS__)
 77#endif
 78
 79#if __has_builtin(__builtin___vsnprintf_chk)
 80#define __vsnprintf_chk_func(str, len, flag, format, ap) \
 81	__builtin___vsnprintf_chk (str, len, flag, __darwin_obsz(str), format, ap)
 82#endif
 83
 84
 85#if __has_builtin(__builtin___sprintf_chk)
 86#define __sprintf_chk_func(str, flag, ...) \
 87	__builtin___sprintf_chk (str, flag, __darwin_obsz(str), __VA_ARGS__)
 88#endif
 89
 90#if __has_builtin(__builtin___vsprintf_chk)
 91#define __vsprintf_chk_func(str, flag, format, ap) \
 92	__builtin___vsprintf_chk (str, flag, __darwin_obsz(str), format, ap)
 93#endif
 94
 95
 96#ifdef __undef__has_builtin
 97#undef __undef__has_builtin
 98#undef __has_builtin
 99#endif
100
101#endif
102
103/* sprintf, vsprintf, snprintf, vsnprintf */
104
105#ifdef __sprintf_chk_func
106#undef sprintf
107#define sprintf(str, ...) __sprintf_chk_func (str, 0, __VA_ARGS__)
108#endif
109
110#if __DARWIN_C_LEVEL >= 200112L
111
112#ifdef __vsprintf_chk_func
113#undef vsprintf
114#define vsprintf(str, ...) __vsprintf_chk_func (str, 0, __VA_ARGS__)
115#endif
116
117#ifdef __snprintf_chk_func
118#undef snprintf
119#define snprintf(str, len, ...) __snprintf_chk_func (str, len, 0, __VA_ARGS__)
120#endif
121
122#ifdef __vsnprintf_chk_func
123#undef vsnprintf
124#define vsnprintf(str, len, ...) __vsnprintf_chk_func (str, len, 0, __VA_ARGS__)
125#endif
126
127#endif
128
129#endif /* _USE_FORTIFY_LEVEL > 0 */
130#endif /* _SECURE__STDIO_H_ */