2 //===----------------------------------------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #ifndef KMP_SAFE_C_API_H
11 #define KMP_SAFE_C_API_H
13 #include "kmp_platform.h"
16 // Replacement for banned C API
18 // Not every unsafe call listed here is handled now, but keeping everything
19 // in one place should be handy for future maintenance.
20 #if KMP_OS_WINDOWS && KMP_MSVC_COMPAT
22 #define RSIZE_MAX_STR (4UL << 10) // 4KB
24 // _malloca was suggested, but it is not a drop-in replacement for _alloca
25 #define KMP_ALLOCA _alloca
27 #define KMP_MEMCPY_S memcpy_s
28 #define KMP_SNPRINTF sprintf_s
29 #define KMP_SSCANF sscanf_s
30 #define KMP_STRCPY_S strcpy_s
31 #define KMP_STRNCPY_S strncpy_s
33 // Use this only when buffer size is unknown
34 #define KMP_MEMCPY(dst, src, cnt) memcpy_s(dst, cnt, src, cnt)
36 #define KMP_STRLEN(str) strnlen_s(str, RSIZE_MAX_STR)
38 // Use this only when buffer size is unknown
39 #define KMP_STRNCPY(dst, src, cnt) strncpy_s(dst, cnt, src, cnt)
41 // _TRUNCATE insures buffer size > max string to print.
42 #define KMP_VSNPRINTF(dst, cnt, fmt, arg) \
43 vsnprintf_s(dst, cnt, _TRUNCATE, fmt, arg)
45 #else // KMP_OS_WINDOWS
47 // For now, these macros use the existing API.
49 #define KMP_ALLOCA alloca
50 #define KMP_MEMCPY_S(dst, bsz, src, cnt) memcpy(dst, src, cnt)
51 #define KMP_SNPRINTF snprintf
52 #define KMP_SSCANF sscanf
53 #define KMP_STRCPY_S(dst, bsz, src) strcpy(dst, src)
54 #define KMP_STRNCPY_S(dst, bsz, src, cnt) strncpy(dst, src, cnt)
55 #define KMP_VSNPRINTF vsnprintf
56 #define KMP_STRNCPY strncpy
57 #define KMP_STRLEN strlen
58 #define KMP_MEMCPY memcpy
60 #endif // KMP_OS_WINDOWS
62 // Offer truncated version of strncpy
63 static inline void __kmp_strncpy_truncate(char *buffer, size_t buf_size,
64 char const *src, size_t src_size) {
65 if (src_size >= buf_size) {
66 src_size = buf_size - 1;
67 KMP_STRNCPY_S(buffer, buf_size, src, src_size);
68 buffer[buf_size - 1] = '\0';
70 KMP_STRNCPY_S(buffer, buf_size, src, src_size);
74 #endif // KMP_SAFE_C_API_H