[Openmp-commits] [PATCH] D143401: [OpenMP][libomp] Remove false positive for memory sanitizer

Jonathan Peyton via Phabricator via Openmp-commits openmp-commits at lists.llvm.org
Mon Feb 6 11:38:34 PST 2023


jlpeyton updated this revision to Diff 495223.
jlpeyton added a comment.

Apologies for the breakage!

Update patch to consider that buf.used may be "downsized" without setting a null byte. Hence, `buf.str + buf.used` may not point to a null-byte. If we insert a null-byte forcefully for `__kmp_str_buf_cat[buf]()` then the `strncat()` works as expected as well as preventing false positives from the memory sanitizer.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D143401/new/

https://reviews.llvm.org/D143401

Files:
  openmp/runtime/src/kmp_safe_c_api.h
  openmp/runtime/src/kmp_str.cpp


Index: openmp/runtime/src/kmp_str.cpp
===================================================================
--- openmp/runtime/src/kmp_str.cpp
+++ openmp/runtime/src/kmp_str.cpp
@@ -137,8 +137,8 @@
   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 @@
   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
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
@@ -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 @@
 #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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D143401.495223.patch
Type: text/x-patch
Size: 1792 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/openmp-commits/attachments/20230206/7a359340/attachment.bin>


More information about the Openmp-commits mailing list