/*
- * Copyright (c) 2007 Apple Inc. All rights reserved.
+ * Copyright (c) 2007,2017 Apple Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
#define _SECURE__STRING_H_
#include <sys/cdefs.h>
+#include <Availability.h>
#include <secure/_common.h>
#if _USE_FORTIFY_LEVEL > 0
-/* memcpy, mempcpy, memmove, memset, strcpy, stpcpy, strncpy, strcat
- and strncat */
+/* <rdar://problem/12622659> */
+#if defined(__clang__) && \
+ ((defined(__apple_build_version__) && __apple_build_version__ >= 4260006) || \
+ (!defined(__apple_build_version__) && (__clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >= 3))))
+#define __HAS_FIXED_CHK_PROTOTYPES 1
+#else
+#define __HAS_FIXED_CHK_PROTOTYPES 0
+#endif
+
+/* memccpy, memcpy, mempcpy, memmove, memset, strcpy, strlcpy, stpcpy,
+ strncpy, stpncpy, strcat, strlcat, and strncat */
+#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000 || __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090 || \
+ defined(__DRIVERKIT_VERSION_MIN_REQUIRED)
+#if __has_builtin(__builtin___memccpy_chk) && __HAS_FIXED_CHK_PROTOTYPES
+#undef memccpy
+/* void *memccpy(void *dst, const void *src, int c, size_t n) */
+#define memccpy(dest, ...) \
+ __builtin___memccpy_chk (dest, __VA_ARGS__, __darwin_obsz0 (dest))
+#endif
+#endif
+
+#if __has_builtin(__builtin___memcpy_chk) || defined(__GNUC__)
#undef memcpy
+/* void *memcpy(void *dst, const void *src, size_t n) */
+#define memcpy(dest, ...) \
+ __builtin___memcpy_chk (dest, __VA_ARGS__, __darwin_obsz0 (dest))
+#endif
+
+#if __has_builtin(__builtin___memmove_chk) || defined(__GNUC__)
#undef memmove
+/* void *memmove(void *dst, const void *src, size_t len) */
+#define memmove(dest, ...) \
+ __builtin___memmove_chk (dest, __VA_ARGS__, __darwin_obsz0 (dest))
+#endif
+
+#if __has_builtin(__builtin___memset_chk) || defined(__GNUC__)
#undef memset
+/* void *memset(void *b, int c, size_t len) */
+#define memset(dest, ...) \
+ __builtin___memset_chk (dest, __VA_ARGS__, __darwin_obsz0 (dest))
+#endif
+
+#ifndef UNIFDEF_DRIVERKIT
+#if __has_builtin(__builtin___strcpy_chk) || defined(__GNUC__)
#undef strcpy
-#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
+/* char *strcpy(char *dst, const char *src) */
+#define strcpy(dest, ...) \
+ __builtin___strcpy_chk (dest, __VA_ARGS__, __darwin_obsz (dest))
+#endif
+
+#if __DARWIN_C_LEVEL >= 200809L
+#if __has_builtin(__builtin___stpcpy_chk) || defined(__GNUC__)
#undef stpcpy
-#endif /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
+/* char *stpcpy(char *dst, const char *src) */
+#define stpcpy(dest, ...) \
+ __builtin___stpcpy_chk (dest, __VA_ARGS__, __darwin_obsz (dest))
+#endif
+#endif /* __DARWIN_C_LEVEL >= 200809L */
+#endif /* UNIFDEF_DRIVERKIT */
+
+#if __DARWIN_C_LEVEL >= 200809L
+#if __has_builtin(__builtin___stpncpy_chk) || __APPLE_CC__ >= 5666 || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)
+#undef stpncpy
+/* char *stpncpy(char *dst, const char *src, size_t n) */
+#define stpncpy(dest, ...) \
+ __builtin___stpncpy_chk (dest, __VA_ARGS__, __darwin_obsz (dest))
+#endif
+#endif /* _DARWIN_C_LEVEL >= 200809L */
+
+#if __DARWIN_C_LEVEL >= __DARWIN_C_FULL
+#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000 || __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090 || \
+ defined(__DRIVERKIT_VERSION_MIN_REQUIRED)
+#if __has_builtin(__builtin___strlcpy_chk) && __HAS_FIXED_CHK_PROTOTYPES
+#undef strlcpy
+/* size_t strlcpy(char *dst, const char *source, size_t size) */
+#define strlcpy(dest, ...) \
+ __builtin___strlcpy_chk (dest, __VA_ARGS__, __darwin_obsz (dest))
+#endif
+
+#if __has_builtin(__builtin___strlcat_chk) && __HAS_FIXED_CHK_PROTOTYPES
+#undef strlcat
+/* size_t strlcat(char *dst, const char *source, size_t size) */
+#define strlcat(dest, ...) \
+ __builtin___strlcat_chk (dest, __VA_ARGS__, __darwin_obsz (dest))
+#endif
+#endif /* __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000 || __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090 */
+#endif /* __DARWIN_C_LEVEL >= __DARWIN_C_FULL */
+
+#if __has_builtin(__builtin___strncpy_chk) || defined(__GNUC__)
#undef strncpy
-#undef strcat
-#undef strncat
+/* char *strncpy(char *dst, const char *src, size_t n) */
+#define strncpy(dest, ...) \
+ __builtin___strncpy_chk (dest, __VA_ARGS__, __darwin_obsz (dest))
+#endif
-#define memcpy(dest, src, len) \
- ((__darwin_obsz0 (dest) != (size_t) -1) \
- ? __builtin___memcpy_chk (dest, src, len, __darwin_obsz0 (dest)) \
- : __inline_memcpy_chk (dest, src, len))
-
-static __inline void *
-__inline_memcpy_chk (void *__dest, const void *__src, size_t __len)
-{
- return __builtin___memcpy_chk (__dest, __src, __len, __darwin_obsz0(__dest));
-}
-
-#define memmove(dest, src, len) \
- ((__darwin_obsz0 (dest) != (size_t) -1) \
- ? __builtin___memmove_chk (dest, src, len, __darwin_obsz0 (dest)) \
- : __inline_memmove_chk (dest, src, len))
-
-static __inline void *
-__inline_memmove_chk (void *__dest, const void *__src, size_t __len)
-{
- return __builtin___memmove_chk (__dest, __src, __len, __darwin_obsz0(__dest));
-}
-
-#define memset(dest, val, len) \
- ((__darwin_obsz0 (dest) != (size_t) -1) \
- ? __builtin___memset_chk (dest, val, len, __darwin_obsz0 (dest)) \
- : __inline_memset_chk (dest, val, len))
-
-static __inline void *
-__inline_memset_chk (void *__dest, int __val, size_t __len)
-{
- return __builtin___memset_chk (__dest, __val, __len, __darwin_obsz0(__dest));
-}
-
-#define strcpy(dest, src) \
- ((__darwin_obsz0 (dest) != (size_t) -1) \
- ? __builtin___strcpy_chk (dest, src, __darwin_obsz (dest)) \
- : __inline_strcpy_chk (dest, src))
-
-static __inline char *
-__inline_strcpy_chk (char *__restrict __dest, const char *__restrict __src)
-{
- return __builtin___strcpy_chk (__dest, __src, __darwin_obsz(__dest));
-}
-
-#if !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
-#define stpcpy(dest, src) \
- ((__darwin_obsz0 (dest) != (size_t) -1) \
- ? __builtin___stpcpy_chk (dest, src, __darwin_obsz (dest)) \
- : __inline_stpcpy_chk (dest, src))
-
-static __inline char *
-__inline_stpcpy_chk (char *__dest, const char *__src)
-{
- return __builtin___stpcpy_chk (__dest, __src, __darwin_obsz(__dest));
-}
-#endif /* (!_POSIX_C_SOURCE || _DARWIN_C_SOURCE) */
-
-#define strncpy(dest, src, len) \
- ((__darwin_obsz0 (dest) != (size_t) -1) \
- ? __builtin___strncpy_chk (dest, src, len, __darwin_obsz (dest)) \
- : __inline_strncpy_chk (dest, src, len))
-
-static __inline char *
-__inline_strncpy_chk (char *__restrict __dest, const char *__restrict __src,
- size_t __len)
-{
- return __builtin___strncpy_chk (__dest, __src, __len, __darwin_obsz(__dest));
-}
-
-#define strcat(dest, src) \
- ((__darwin_obsz0 (dest) != (size_t) -1) \
- ? __builtin___strcat_chk (dest, src, __darwin_obsz (dest)) \
- : __inline_strcat_chk (dest, src))
-
-static __inline char *
-__inline_strcat_chk (char *__restrict __dest, const char *__restrict __src)
-{
- return __builtin___strcat_chk (__dest, __src, __darwin_obsz(__dest));
-}
-
-#define strncat(dest, src, len) \
- ((__darwin_obsz0 (dest) != (size_t) -1) \
- ? __builtin___strncat_chk (dest, src, len, __darwin_obsz (dest)) \
- : __inline_strncat_chk (dest, src, len))
-
-static __inline char *
-__inline_strncat_chk (char *__restrict __dest, const char *__restrict __src,
- size_t __len)
-{
- return __builtin___strncat_chk (__dest, __src, __len, __darwin_obsz(__dest));
-}
+#ifndef UNIFDEF_DRIVERKIT
+#if __has_builtin(__builtin___strcat_chk) || defined(__GNUC__)
+#undef strcat
+/* char *strcat(char *s1, const char *s2) */
+#define strcat(dest, ...) \
+ __builtin___strcat_chk (dest, __VA_ARGS__, __darwin_obsz (dest))
+#endif
+#endif /* UNIFDEF_DRIVERKIT */
+#if ! (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && __IPHONE_OS_VERSION_MIN_REQUIRED < 32000)
+#if __has_builtin(__builtin___strncat_chk) || defined(__GNUC__)
+#undef strncat
+/* char *strncat(char *s1, const char *s2, size_t n) */
+#define strncat(dest, ...) \
+ __builtin___strncat_chk (dest, __VA_ARGS__, __darwin_obsz (dest))
#endif
#endif
+
+#undef __HAS_FIXED_CHK_PROTOTYPES
+
+#endif /* _USE_FORTIFY_LEVEL > 0 */
+#endif /* _SECURE__STRING_H_ */