xref: /freebsd/contrib/llvm-project/openmp/runtime/src/kmp_safe_c_api.h (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
1 
2 //===----------------------------------------------------------------------===//
3 //
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
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef KMP_SAFE_C_API_H
11 #define KMP_SAFE_C_API_H
12 
13 #include "kmp_platform.h"
14 #include <string.h>
15 
16 // Replacement for banned C API
17 
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
21 
22 #define RSIZE_MAX_STR (4UL << 10) // 4KB
23 
24 // _malloca was suggested, but it is not a drop-in replacement for _alloca
25 #define KMP_ALLOCA _alloca
26 
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
32 
33 // Use this only when buffer size is unknown
34 #define KMP_MEMCPY(dst, src, cnt) memcpy_s(dst, cnt, src, cnt)
35 
36 #define KMP_STRLEN(str) strnlen_s(str, RSIZE_MAX_STR)
37 
38 // Use this only when buffer size is unknown
39 #define KMP_STRNCPY(dst, src, cnt) strncpy_s(dst, cnt, src, cnt)
40 
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)
44 
45 #else // KMP_OS_WINDOWS
46 
47 // For now, these macros use the existing API.
48 
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
59 
60 #endif // KMP_OS_WINDOWS
61 
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';
69   } else {
70     KMP_STRNCPY_S(buffer, buf_size, src, src_size);
71   }
72 }
73 
74 #endif // KMP_SAFE_C_API_H
75