[compiler-rt] r186155 - [Sanitizer] Revert some parts of r186109 related to mbstowcs-and-friends interceptors
Alexey Samsonov
samsonov at google.com
Fri Jul 12 04:59:59 PDT 2013
Author: samsonov
Date: Fri Jul 12 06:59:58 2013
New Revision: 186155
URL: http://llvm.org/viewvc/llvm-project?rev=186155&view=rev
Log:
[Sanitizer] Revert some parts of r186109 related to mbstowcs-and-friends interceptors
Modified:
compiler-rt/trunk/lib/msan/tests/msan_test.cc
compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc
Modified: compiler-rt/trunk/lib/msan/tests/msan_test.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/msan/tests/msan_test.cc?rev=186155&r1=186154&r2=186155&view=diff
==============================================================================
--- compiler-rt/trunk/lib/msan/tests/msan_test.cc (original)
+++ compiler-rt/trunk/lib/msan/tests/msan_test.cc Fri Jul 12 06:59:58 2013
@@ -1210,6 +1210,19 @@ TEST(MemorySanitizer, asprintf) { // NO
free(pbuf);
}
+TEST(MemorySanitizer, mbstowcs) {
+ const char *x = "abc";
+ wchar_t buff[10];
+ int res = mbstowcs(buff, x, 2);
+ EXPECT_EQ(2, res);
+ EXPECT_EQ(L'a', buff[0]);
+ EXPECT_EQ(L'b', buff[1]);
+ EXPECT_POISONED(buff[2]);
+ res = mbstowcs(buff, x, 10);
+ EXPECT_EQ(3, res);
+ EXPECT_NOT_POISONED(buff[3]);
+}
+
TEST(MemorySanitizer, wcstombs) {
const wchar_t *x = L"abc";
char buff[10];
Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc?rev=186155&r1=186154&r2=186155&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc Fri Jul 12 06:59:58 2013
@@ -1540,8 +1540,10 @@ INTERCEPTOR(SIZE_T, mbstowcs, wchar_t *d
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, mbstowcs, dest, src, len);
SIZE_T res = REAL(mbstowcs)(dest, src, len);
- if (res != (SIZE_T) - 1 && dest)
- COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, (res + 1) * sizeof(wchar_t));
+ if (res != (SIZE_T)-1 && dest) {
+ SIZE_T write_cnt = res + (res < len);
+ COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
+ }
return res;
}
@@ -1549,12 +1551,13 @@ INTERCEPTOR(SIZE_T, mbsrtowcs, wchar_t *
void *ps) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, mbsrtowcs, dest, src, len, ps);
- if (src) {
- COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
- }
+ COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
SIZE_T res = REAL(mbsrtowcs)(dest, src, len, ps);
- if (res != (SIZE_T) - 1 && dest)
- COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, (res + 1) * sizeof(wchar_t));
+ if (res != (SIZE_T)-1 && dest) {
+ // Terminating '\0' is not printed iff *src is cleared.
+ SIZE_T write_cnt = res + !(*src);
+ COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
+ }
return res;
}
@@ -1570,13 +1573,16 @@ INTERCEPTOR(SIZE_T, mbsnrtowcs, wchar_t
SIZE_T len, void *ps) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, mbsnrtowcs, dest, src, nms, len, ps);
- if (src) {
+ if (nms) {
+ COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
- if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
}
SIZE_T res = REAL(mbsnrtowcs)(dest, src, nms, len, ps);
- if (res != (SIZE_T) - 1 && dest)
- COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, (res + 1) * sizeof(wchar_t));
+ if (res != (SIZE_T)-1 && dest && nms) {
+ // Terminating '\0' is not printed iff *src is cleared.
+ SIZE_T write_cnt = res + !(*src);
+ COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
+ }
return res;
}
@@ -1590,8 +1596,10 @@ INTERCEPTOR(SIZE_T, wcstombs, char *dest
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, wcstombs, dest, src, len);
SIZE_T res = REAL(wcstombs)(dest, src, len);
- if (res != (SIZE_T) - 1 && dest)
- COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, res + 1);
+ if (res != (SIZE_T)-1 && dest) {
+ SIZE_T write_cnt = res + (res < len);
+ COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
+ }
return res;
}
@@ -1599,12 +1607,13 @@ INTERCEPTOR(SIZE_T, wcsrtombs, char *des
void *ps) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, wcsrtombs, dest, src, len, ps);
- if (src) {
- COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
- }
+ COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
SIZE_T res = REAL(wcsrtombs)(dest, src, len, ps);
- if (res != (SIZE_T) - 1 && dest)
- COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, res + 1);
+ if (res != (SIZE_T)-1 && dest) {
+ // Terminating '\0' is not printed iff *src is cleared.
+ SIZE_T write_cnt = res + !(*src);
+ COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
+ }
return res;
}
@@ -1620,13 +1629,16 @@ INTERCEPTOR(SIZE_T, wcsnrtombs, char *de
SIZE_T len, void *ps) {
void *ctx;
COMMON_INTERCEPTOR_ENTER(ctx, wcsnrtombs, dest, src, nms, len, ps);
- if (src) {
+ if (nms) {
COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
- if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
+ COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
}
SIZE_T res = REAL(wcsnrtombs)(dest, src, nms, len, ps);
- if (res != (SIZE_T) - 1 && dest)
- COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, res + 1);
+ if (res != (SIZE_T)-1 && dest && nms) {
+ // Terminating '\0' is not printed iff *src is cleared.
+ SIZE_T write_cnt = res + !(*src);
+ COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
+ }
return res;
}
More information about the llvm-commits
mailing list