kmp_safe_c_api.h 2.5 KB

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