blob: 9d0da0b6073d1d891ca49f8ea0c454a4bde1ef28 [file] [log] [blame]
Andrey Churbanov74bf17b2015-04-02 13:27:08 +00001
2//===----------------------------------------------------------------------===//
3//
4// The LLVM Compiler Infrastructure
5//
6// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.txt for details.
8//
9//===----------------------------------------------------------------------===//
10
Andrey Churbanov74bf17b2015-04-02 13:27:08 +000011#ifndef KMP_SAFE_C_API_H
12#define KMP_SAFE_C_API_H
13
Andrey Churbanov74bf17b2015-04-02 13:27:08 +000014// Replacement for banned C API
Andrey Churbanov74bf17b2015-04-02 13:27:08 +000015
16// Not every unsafe call listed here is handled now, but keeping everything
17// in one place should be handy for future maintenance.
18#if KMP_OS_WINDOWS
19
Jonathan Peyton30419822017-05-12 18:01:32 +000020#define RSIZE_MAX_STR (4UL << 10) // 4KB
Andrey Churbanov74bf17b2015-04-02 13:27:08 +000021
22// _malloca was suggested, but it is not a drop-in replacement for _alloca
Jonathan Peyton30419822017-05-12 18:01:32 +000023#define KMP_ALLOCA _alloca
Andrey Churbanov74bf17b2015-04-02 13:27:08 +000024
Jonathan Peyton30419822017-05-12 18:01:32 +000025#define KMP_MEMCPY_S memcpy_s
26#define KMP_SNPRINTF sprintf_s
27#define KMP_SSCANF sscanf_s
28#define KMP_STRCPY_S strcpy_s
29#define KMP_STRNCPY_S strncpy_s
Andrey Churbanov74bf17b2015-04-02 13:27:08 +000030
31// Use this only when buffer size is unknown
Jonathan Peyton30419822017-05-12 18:01:32 +000032#define KMP_MEMCPY(dst, src, cnt) memcpy_s(dst, cnt, src, cnt)
Andrey Churbanov74bf17b2015-04-02 13:27:08 +000033
Jonathan Peyton30419822017-05-12 18:01:32 +000034#define KMP_STRLEN(str) strnlen_s(str, RSIZE_MAX_STR)
Andrey Churbanov74bf17b2015-04-02 13:27:08 +000035
36// Use this only when buffer size is unknown
Jonathan Peyton30419822017-05-12 18:01:32 +000037#define KMP_STRNCPY(dst, src, cnt) strncpy_s(dst, cnt, src, cnt)
Andrey Churbanov74bf17b2015-04-02 13:27:08 +000038
39// _TRUNCATE insures buffer size > max string to print.
Jonathan Peyton30419822017-05-12 18:01:32 +000040#define KMP_VSNPRINTF(dst, cnt, fmt, arg) \
41 vsnprintf_s(dst, cnt, _TRUNCATE, fmt, arg)
Andrey Churbanov74bf17b2015-04-02 13:27:08 +000042
43#else // KMP_OS_WINDOWS
44
45// For now, these macros use the existing API.
46
Jonathan Peyton30419822017-05-12 18:01:32 +000047#define KMP_ALLOCA alloca
48#define KMP_MEMCPY_S(dst, bsz, src, cnt) memcpy(dst, src, cnt)
49#define KMP_SNPRINTF snprintf
50#define KMP_SSCANF sscanf
51#define KMP_STRCPY_S(dst, bsz, src) strcpy(dst, src)
52#define KMP_STRNCPY_S(dst, bsz, src, cnt) strncpy(dst, src, cnt)
53#define KMP_VSNPRINTF vsnprintf
54#define KMP_STRNCPY strncpy
55#define KMP_STRLEN strlen
56#define KMP_MEMCPY memcpy
Andrey Churbanov74bf17b2015-04-02 13:27:08 +000057
58#endif // KMP_OS_WINDOWS
59
60#endif // KMP_SAFE_C_API_H