[sanitizer] More checks in mbstowcs-like interceptors.


git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@186004 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/msan/tests/msan_test.cc b/lib/msan/tests/msan_test.cc
index 82da50e..b49df52 100644
--- a/lib/msan/tests/msan_test.cc
+++ b/lib/msan/tests/msan_test.cc
@@ -1225,6 +1225,7 @@
   const wchar_t *p = x;
   char buff[10];
   mbstate_t mbs;
+  memset(&mbs, 0, sizeof(mbs));
   int res = wcsrtombs(buff, &p, 4, &mbs);
   EXPECT_EQ(res, 3);
   EXPECT_EQ(buff[0], 'a');
@@ -1239,6 +1240,7 @@
   const wchar_t *p = x;
   char buff[10];
   mbstate_t mbs;
+  memset(&mbs, 0, sizeof(mbs));
   int res = wcsnrtombs(buff, &p, 2, 4, &mbs);
   EXPECT_EQ(res, 2);
   EXPECT_EQ(buff[0], 'a');
diff --git a/lib/sanitizer_common/sanitizer_common_interceptors.inc b/lib/sanitizer_common/sanitizer_common_interceptors.inc
index 7899204..033f816 100644
--- a/lib/sanitizer_common/sanitizer_common_interceptors.inc
+++ b/lib/sanitizer_common/sanitizer_common_interceptors.inc
@@ -1553,9 +1553,8 @@
             void *ps) {
   void *ctx;
   COMMON_INTERCEPTOR_ENTER(ctx, mbsrtowcs, dest, src, len, ps);
-  if (src) {
-    COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
-  }
+  if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
+  if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
   SIZE_T res = REAL(mbsrtowcs)(dest, src, len, ps);
   if (res != (SIZE_T)(-1) && dest && src) {
     // This function, and several others, may or may not write the terminating
@@ -1582,6 +1581,7 @@
     COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
     if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
   }
+  if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
   SIZE_T res = REAL(mbsnrtowcs)(dest, src, nms, len, ps);
   if (res != (SIZE_T)(-1) && dest && src) {
     SIZE_T write_cnt = res + !*src;
@@ -1611,9 +1611,8 @@
             void *ps) {
   void *ctx;
   COMMON_INTERCEPTOR_ENTER(ctx, wcsrtombs, dest, src, len, ps);
-  if (src) {
-    COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
-  }
+  if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
+  if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
   SIZE_T res = REAL(wcsrtombs)(dest, src, len, ps);
   if (res != (SIZE_T) - 1 && dest && src) {
     SIZE_T write_cnt = res + !*src;
@@ -1638,6 +1637,7 @@
     COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
     if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
   }
+  if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
   SIZE_T res = REAL(wcsnrtombs)(dest, src, nms, len, ps);
   if (res != (SIZE_T) - 1 && dest && src) {
     SIZE_T write_cnt = res + !*src;
diff --git a/lib/sanitizer_common/sanitizer_platform_limits_posix.cc b/lib/sanitizer_common/sanitizer_platform_limits_posix.cc
index 0876283..93d0d7f 100644
--- a/lib/sanitizer_common/sanitizer_platform_limits_posix.cc
+++ b/lib/sanitizer_common/sanitizer_platform_limits_posix.cc
@@ -39,6 +39,7 @@
 #include <sys/utsname.h>
 #include <termios.h>
 #include <time.h>
+#include <wchar.h>
 
 #if SANITIZER_LINUX
 #include <sys/mount.h>
@@ -112,6 +113,7 @@
   unsigned pid_t_sz = sizeof(pid_t);
   unsigned timeval_sz = sizeof(timeval);
   unsigned uid_t_sz = sizeof(uid_t);
+  unsigned mbstate_t_sz = sizeof(mbstate_t);
 
 #if !SANITIZER_ANDROID
   unsigned ucontext_t_sz = sizeof(ucontext_t);
diff --git a/lib/sanitizer_common/sanitizer_platform_limits_posix.h b/lib/sanitizer_common/sanitizer_platform_limits_posix.h
index 4e85fbd..6b91fca 100644
--- a/lib/sanitizer_common/sanitizer_platform_limits_posix.h
+++ b/lib/sanitizer_common/sanitizer_platform_limits_posix.h
@@ -32,6 +32,7 @@
   extern unsigned pid_t_sz;
   extern unsigned timeval_sz;
   extern unsigned uid_t_sz;
+  extern unsigned mbstate_t_sz;
 
 #if !SANITIZER_ANDROID
   extern unsigned ucontext_t_sz;