Skip to content

Commit 1d028b8

Browse files
author
Alexey Samsonov
committed
[Sanitizer] Fix false positive in snprintf interceptor - take the number of actually written symbols from real snprintf call.
llvm-svn: 199899
1 parent 793f1b2 commit 1d028b8

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

compiler-rt/lib/asan/lit_tests/TestCases/printf-1.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@ int main() {
99
volatile int x = 12;
1010
volatile float f = 1.239;
1111
volatile char s[] = "34";
12-
printf("%c %d %.3f %s\n", c, x, f, s);
13-
return 0;
1412
// Check that printf works fine under Asan.
13+
printf("%c %d %.3f %s\n", c, x, f, s);
1514
// CHECK: 0 12 1.239 34
15+
// Check that snprintf works fine under Asan.
16+
char buf[4];
17+
snprintf(buf, 1000, "qwe");
18+
printf("%s\n", buf);
19+
// CHECK: qwe
20+
return 0;
1621
}

compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,10 +706,12 @@ FORMAT_INTERCEPTOR_IMPL(__isoc99_sscanf, __isoc99_vsscanf, str, format)
706706
{ \
707707
VPRINTF_INTERCEPTOR_ENTER(vname, str, size, __VA_ARGS__) \
708708
if (common_flags()->check_printf) { \
709-
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, size); \
710709
printf_common(ctx, format, aq); \
711710
} \
712711
int res = REAL(vname)(str, size, __VA_ARGS__); \
712+
if (res >= 0 && common_flags()->check_printf) { \
713+
COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, Min(size, (SIZE_T)(res + 1))); \
714+
} \
713715
VPRINTF_INTERCEPTOR_RETURN(); \
714716
return res; \
715717
}

0 commit comments

Comments
 (0)