[Openmp-commits] [PATCH] D98209: [OpenMP] Fix incorrect KMP_STRLEN() macro on Windows

Jonathan Peyton via Phabricator via Openmp-commits openmp-commits at lists.llvm.org
Mon Mar 8 12:01:49 PST 2021

jlpeyton created this revision.
jlpeyton added reviewers: AndreyChurbanov, tlwilmar, hbae, Nawrin.
jlpeyton added a project: OpenMP.
Herald added subscribers: guansong, yaxunl.
jlpeyton requested review of this revision.
Herald added a reviewer: jdoerfert.
Herald added a subscriber: sstefan1.

The second argument to the `strnlen_s(str, size)` function should be sizeof(str) when str is a true array of characters with known size (instead of just a `char*`).
Use type traits to determine if first parameter is character array and use the correct size based on that trait.

  rG LLVM Github Monorepo



Index: openmp/runtime/src/kmp_safe_c_api.h
--- openmp/runtime/src/kmp_safe_c_api.h
+++ openmp/runtime/src/kmp_safe_c_api.h
@@ -10,6 +10,7 @@
 #ifndef KMP_SAFE_C_API_H
 #define KMP_SAFE_C_API_H
+#include <type_traits>
 #include "kmp_platform.h"
 #include <string.h>
@@ -33,7 +34,15 @@
 // Use this only when buffer size is unknown
 #define KMP_MEMCPY(dst, src, cnt) memcpy_s(dst, cnt, src, cnt)
-#define KMP_STRLEN(str) strnlen_s(str, RSIZE_MAX_STR)
+template <typename T, bool B = std::is_array<T>::value>
+struct kmp_get_rmax_t {};
+template <typename T> struct kmp_get_rmax_t<T, false> {
+  static const size_t value = RSIZE_MAX_STR;
+template <typename T> struct kmp_get_rmax_t<T, true> {
+  static const size_t value = sizeof(T);
+#define KMP_STRLEN(str) strnlen_s(str, kmp_get_rmax_t<decltype(str)>::value)
 // Use this only when buffer size is unknown
 #define KMP_STRNCPY(dst, src, cnt) strncpy_s(dst, cnt, src, cnt)
Index: openmp/runtime/src/kmp_os.h
--- openmp/runtime/src/kmp_os.h
+++ openmp/runtime/src/kmp_os.h
@@ -1040,6 +1040,9 @@
 } // extern "C"
 #endif // __cplusplus
+// Safe C API
+#include "kmp_safe_c_api.h"
 // Macros for C++11 atomic functions
 #define KMP_ATOMIC_LD(p, order) (p)->load(std::memory_order_##order)
 #define KMP_ATOMIC_OP(op, p, v, order) (p)->op(v, std::memory_order_##order)
@@ -1090,5 +1093,3 @@
 #endif /* KMP_OS_H */
-// Safe C API
-#include "kmp_safe_c_api.h"

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D98209.329091.patch
Type: text/x-patch
Size: 1570 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/openmp-commits/attachments/20210308/f4a9bbe4/attachment.bin>

More information about the Openmp-commits mailing list