[Openmp-commits] [openmp] 2aebb7c - [OpenMP] Fix incorrect KMP_STRLEN() macro

via Openmp-commits openmp-commits at lists.llvm.org
Mon Apr 5 07:03:48 PDT 2021


Author: Peyton, Jonathan L
Date: 2021-04-05T09:03:09-05:00
New Revision: 2aebb7cb3c88b1446515563653c821e8165b3aaf

URL: https://github.com/llvm/llvm-project/commit/2aebb7cb3c88b1446515563653c821e8165b3aaf
DIFF: https://github.com/llvm/llvm-project/commit/2aebb7cb3c88b1446515563653c821e8165b3aaf.diff

LOG: [OpenMP] Fix incorrect KMP_STRLEN() macro

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 a character array and use the correct size based on that
trait.

Differential Revision: https://reviews.llvm.org/D98209

Added: 
    

Modified: 
    openmp/runtime/src/kmp_os.h
    openmp/runtime/src/kmp_safe_c_api.h

Removed: 
    


################################################################################
diff  --git a/openmp/runtime/src/kmp_os.h b/openmp/runtime/src/kmp_os.h
index 563ed8f7c25f4..fe02ccfae06ff 100644
--- a/openmp/runtime/src/kmp_os.h
+++ b/openmp/runtime/src/kmp_os.h
@@ -1040,6 +1040,9 @@ enum kmp_warnings_level {
 } // 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 @@ extern void *__kmp_lookup_symbol(const char *name);
 #endif
 
 #endif /* KMP_OS_H */
-// Safe C API
-#include "kmp_safe_c_api.h"

diff  --git a/openmp/runtime/src/kmp_safe_c_api.h b/openmp/runtime/src/kmp_safe_c_api.h
index abc0a16f87cfc..3db1ada37b07b 100644
--- a/openmp/runtime/src/kmp_safe_c_api.h
+++ b/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)


        


More information about the Openmp-commits mailing list