Skip to content

Commit 402981e

Browse files
committed
[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 llvm#60501
1 parent e4022b6 commit 402981e

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

openmp/runtime/src/kmp_safe_c_api.h

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#define KMP_SSCANF sscanf_s
3131
#define KMP_STRCPY_S strcpy_s
3232
#define KMP_STRNCPY_S strncpy_s
33+
#define KMP_STRNCAT_S strncat_s
3334

3435
// Use this only when buffer size is unknown
3536
#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> {
6162
#define KMP_SSCANF sscanf
6263
#define KMP_STRCPY_S(dst, bsz, src) strcpy(dst, src)
6364
#define KMP_STRNCPY_S(dst, bsz, src, cnt) strncpy(dst, src, cnt)
65+
#define KMP_STRNCAT_S(dst, bsz, src, cnt) strncat(dst, src, cnt)
6466
#define KMP_VSNPRINTF vsnprintf
6567
#define KMP_STRNCPY strncpy
6668
#define KMP_STRLEN strlen

openmp/runtime/src/kmp_str.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,7 @@ void __kmp_str_buf_cat(kmp_str_buf_t *buffer, char const *str, size_t len) {
137137
KMP_DEBUG_ASSERT(len >= 0);
138138

139139
__kmp_str_buf_reserve(buffer, buffer->used + len + 1);
140-
KMP_MEMCPY(buffer->str + buffer->used, str, len);
141-
buffer->str[buffer->used + len] = 0;
140+
KMP_STRNCAT_S(buffer->str + buffer->used, len + 1, str, len);
142141
__kmp_type_convert(buffer->used + len, &(buffer->used));
143142
KMP_STR_BUF_INVARIANT(buffer);
144143
} // __kmp_str_buf_cat
@@ -151,8 +150,7 @@ void __kmp_str_buf_catbuf(kmp_str_buf_t *dest, const kmp_str_buf_t *src) {
151150
if (!src->str || !src->used)
152151
return;
153152
__kmp_str_buf_reserve(dest, dest->used + src->used + 1);
154-
KMP_MEMCPY(dest->str + dest->used, src->str, src->used);
155-
dest->str[dest->used + src->used] = 0;
153+
KMP_STRNCAT_S(dest->str + dest->used, src->used + 1, src->str, src->used);
156154
dest->used += src->used;
157155
KMP_STR_BUF_INVARIANT(dest);
158156
} // __kmp_str_buf_catbuf

0 commit comments

Comments
 (0)