summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Peyton <jonathan.l.peyton@intel.com>2023-02-06 13:32:35 -0600
committerTom Stellard <tstellar@redhat.com>2023-03-02 23:14:31 -0800
commit791a2eccf87ad3375a62153e926e6d8a56e2b437 (patch)
treee5645974ee73ca414a206f13509dac7dae130aaf
parent7917b15d9ef6b5e34936cd02d47c2c6258d8eeb3 (diff)
downloadllvm-791a2eccf87ad3375a62153e926e6d8a56e2b437.tar.gz
[OpenMP][libomp] Remove false positive for memory sanitizer
The memory sanitizer intercepts the memcpy() call but not the direct assignment of last byte to 0. This leads the sanitizer to believe the last byte of a string based on the kmp_str_buf_t type is uninitialized. Hence, the eventual strlen() inside __kmp_env_dump() leads to an use-of-uninitialized-value warning. Using strncat() instead gives the sanitizer the information it needs. Differential Revision: https://reviews.llvm.org/D143401 Fixes #60501 (cherry picked from commit 4ce32d2f12ec0e7a2551e251c1453d4b338cc2a7)
-rw-r--r--openmp/runtime/src/kmp_safe_c_api.h2
-rw-r--r--openmp/runtime/src/kmp_str.cpp8
2 files changed, 6 insertions, 4 deletions
diff --git a/openmp/runtime/src/kmp_safe_c_api.h b/openmp/runtime/src/kmp_safe_c_api.h
index 3db1ada37b07..72f26fd9897d 100644
--- a/openmp/runtime/src/kmp_safe_c_api.h
+++ b/openmp/runtime/src/kmp_safe_c_api.h
@@ -30,6 +30,7 @@
#define KMP_SSCANF sscanf_s
#define KMP_STRCPY_S strcpy_s
#define KMP_STRNCPY_S strncpy_s
+#define KMP_STRNCAT_S strncat_s
// Use this only when buffer size is unknown
#define KMP_MEMCPY(dst, src, cnt) memcpy_s(dst, cnt, src, cnt)
@@ -61,6 +62,7 @@ template <typename T> struct kmp_get_rmax_t<T, true> {
#define KMP_SSCANF sscanf
#define KMP_STRCPY_S(dst, bsz, src) strcpy(dst, src)
#define KMP_STRNCPY_S(dst, bsz, src, cnt) strncpy(dst, src, cnt)
+#define KMP_STRNCAT_S(dst, bsz, src, cnt) strncat(dst, src, cnt)
#define KMP_VSNPRINTF vsnprintf
#define KMP_STRNCPY strncpy
#define KMP_STRLEN strlen
diff --git a/openmp/runtime/src/kmp_str.cpp b/openmp/runtime/src/kmp_str.cpp
index e64f989fbc69..4cba56964a09 100644
--- a/openmp/runtime/src/kmp_str.cpp
+++ b/openmp/runtime/src/kmp_str.cpp
@@ -137,8 +137,8 @@ void __kmp_str_buf_cat(kmp_str_buf_t *buffer, char const *str, size_t len) {
KMP_DEBUG_ASSERT(len >= 0);
__kmp_str_buf_reserve(buffer, buffer->used + len + 1);
- KMP_MEMCPY(buffer->str + buffer->used, str, len);
- buffer->str[buffer->used + len] = 0;
+ buffer->str[buffer->used] = '\0';
+ KMP_STRNCAT_S(buffer->str + buffer->used, len + 1, str, len);
__kmp_type_convert(buffer->used + len, &(buffer->used));
KMP_STR_BUF_INVARIANT(buffer);
} // __kmp_str_buf_cat
@@ -151,8 +151,8 @@ void __kmp_str_buf_catbuf(kmp_str_buf_t *dest, const kmp_str_buf_t *src) {
if (!src->str || !src->used)
return;
__kmp_str_buf_reserve(dest, dest->used + src->used + 1);
- KMP_MEMCPY(dest->str + dest->used, src->str, src->used);
- dest->str[dest->used + src->used] = 0;
+ dest->str[dest->used] = '\0';
+ KMP_STRNCAT_S(dest->str + dest->used, src->used + 1, src->str, src->used);
dest->used += src->used;
KMP_STR_BUF_INVARIANT(dest);
} // __kmp_str_buf_catbuf