diff --git a/compiler-rt/lib/msan/tests/msan_test.cpp b/compiler-rt/lib/msan/tests/msan_test.cpp --- a/compiler-rt/lib/msan/tests/msan_test.cpp +++ b/compiler-rt/lib/msan/tests/msan_test.cpp @@ -3750,6 +3750,14 @@ ASSERT_EQ(-1, n); } +TEST(MemorySanitizer, wordexp_empty) { + wordexp_t w; + int res = wordexp("", &w, 0); + ASSERT_EQ(0, res); + ASSERT_EQ(0U, w.we_wordc); + ASSERT_STREQ(nullptr, w.we_wordv[0]); +} + TEST(MemorySanitizer, wordexp) { wordexp_t w; int res = wordexp("a b c", &w, 0); diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc --- a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc @@ -3998,9 +3998,8 @@ COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p)); uptr we_wordc = ((flags & wordexp_wrde_dooffs) ? p->we_offs : 0) + p->we_wordc; - if (we_wordc) - COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->we_wordv, - sizeof(*p->we_wordv) * we_wordc); + COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->we_wordv, + sizeof(*p->we_wordv) * (we_wordc + 1)); for (uptr i = 0; i < we_wordc; ++i) { char *w = p->we_wordv[i]; if (w) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, w, internal_strlen(w) + 1);