diff options
author | Dmitry Vyukov <dvyukov@google.com> | 2015-04-06 18:00:26 +0000 |
---|---|---|
committer | Dmitry Vyukov <dvyukov@google.com> | 2015-04-06 18:00:26 +0000 |
commit | c2c521c58e2bc2fda2e1a43b9c1d11f5a6bcb700 (patch) | |
tree | 37cae99c7d85f510f16aa730abfd059767e44baf /test/asan/TestCases/strtol_strict.c | |
parent | ee7523e7791f7ff6f1e179dfe6309817c264f0da (diff) | |
download | compiler-rt-c2c521c58e2bc2fda2e1a43b9c1d11f5a6bcb700.tar.gz |
sanitizer: new "strict_string_checks" run-time flag
This patch is related to Issue 346: moar string interceptors: strstr, strcasestr, strcspn, strpbrk
As was suggested in original review http://reviews.llvm.org/D6056 a new "strict_string_checks" run-time flag introduced.
The flag support applied for existing common, asan, msan and tsan interceptors. New asan tests added.
Change by Maria Guseva reviewed in http://reviews.llvm.org/D7123
git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@234187 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'test/asan/TestCases/strtol_strict.c')
-rw-r--r-- | test/asan/TestCases/strtol_strict.c | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/test/asan/TestCases/strtol_strict.c b/test/asan/TestCases/strtol_strict.c new file mode 100644 index 000000000..2ff5028fe --- /dev/null +++ b/test/asan/TestCases/strtol_strict.c @@ -0,0 +1,111 @@ +// Test strict_string_checks option in strtol function +// RUN: %clang_asan -DTEST1 %s -o %t +// RUN: %run %t test1 2>&1 +// RUN: ASAN_OPTIONS=strict_string_checks=false %run %t test1 2>&1 +// RUN: ASAN_OPTIONS=strict_string_checks=true not %run %t test1 2>&1 | FileCheck %s --check-prefix=CHECK1 +// RUN: %run %t test2 2>&1 +// RUN: ASAN_OPTIONS=strict_string_checks=false %run %t test2 2>&1 +// RUN: ASAN_OPTIONS=strict_string_checks=true not %run %t test2 2>&1 | FileCheck %s --check-prefix=CHECK2 +// RUN: %run %t test3 2>&1 +// RUN: ASAN_OPTIONS=strict_string_checks=false %run %t test3 2>&1 +// RUN: ASAN_OPTIONS=strict_string_checks=true not %run %t test3 2>&1 | FileCheck %s --check-prefix=CHECK3 +// RUN: %run %t test4 2>&1 +// RUN: ASAN_OPTIONS=strict_string_checks=false %run %t test4 2>&1 +// RUN: ASAN_OPTIONS=strict_string_checks=true not %run %t test4 2>&1 | FileCheck %s --check-prefix=CHECK4 +// RUN: %run %t test5 2>&1 +// RUN: ASAN_OPTIONS=strict_string_checks=false %run %t test5 2>&1 +// RUN: ASAN_OPTIONS=strict_string_checks=true not %run %t test5 2>&1 | FileCheck %s --check-prefix=CHECK5 +// RUN: %run %t test6 2>&1 +// RUN: ASAN_OPTIONS=strict_string_checks=false %run %t test6 2>&1 +// RUN: ASAN_OPTIONS=strict_string_checks=true not %run %t test6 2>&1 | FileCheck %s --check-prefix=CHECK6 +// RUN: %run %t test7 2>&1 +// RUN: ASAN_OPTIONS=strict_string_checks=false %run %t test7 2>&1 +// RUN: ASAN_OPTIONS=strict_string_checks=true not %run %t test7 2>&1 | FileCheck %s --check-prefix=CHECK7 + +#include <assert.h> +#include <stdlib.h> +#include <string.h> + +void test1(char *array, char *endptr) { + // Buffer overflow if there is no terminating null (depends on base) + long r = strtol(array, &endptr, 3); + assert(array + 2 == endptr); + assert(r == 5); +} + +void test2(char *array, char *endptr) { + // Buffer overflow if there is no terminating null (depends on base) + array[2] = 'z'; + long r = strtol(array, &endptr, 35); + assert(array + 2 == endptr); + assert(r == 37); +} + +void test3(char *array, char *endptr) { + // Buffer overflow if base is invalid. + long r = strtol(array - 1, NULL, -1); + assert(r == 0); +} + +void test4(char *array, char *endptr) { + // Buffer overflow if base is invalid. + long r = strtol(array + 3, NULL, 1); + assert(r == 0); +} + +void test5(char *array, char *endptr) { + // Overflow if no digits are found. + array[0] = ' '; + array[1] = '+'; + array[2] = '-'; + long r = strtol(array, NULL, 0); + assert(r == 0); +} + +void test6(char *array, char *endptr) { + // Overflow if no digits are found. + array[0] = ' '; + array[1] = array[2] = 'z'; + long r = strtol(array, &endptr, 0); + assert(array == endptr); + assert(r == 0); +} + +void test7(char *array, char *endptr) { + // Overflow if no digits are found. + array[2] = 'z'; + long r = strtol(array + 2, NULL, 0); + assert(r == 0); +} + +int main(int argc, char **argv) { + char *array = (char*)malloc(3); + char *endptr = NULL; + array[0] = '1'; + array[1] = '2'; + array[2] = '3'; + if (argc != 2) return 1; + if (!strcmp(argv[1], "test1")) test1(array, endptr); + // CHECK1: {{.*ERROR: AddressSanitizer: heap-buffer-overflow on address}} + // CHECK1: READ of size 4 + if (!strcmp(argv[1], "test2")) test2(array, endptr); + // CHECK2: {{.*ERROR: AddressSanitizer: heap-buffer-overflow on address}} + // CHECK2: READ of size 4 + if (!strcmp(argv[1], "test3")) test3(array, endptr); + // CHECK3: {{.*ERROR: AddressSanitizer: heap-buffer-overflow on address}} + // CHECK3: READ of size 5 + if (!strcmp(argv[1], "test4")) test4(array, endptr); + // CHECK4: {{.*ERROR: AddressSanitizer: heap-buffer-overflow on address}} + // CHECK4: READ of size 1 + if (!strcmp(argv[1], "test5")) test5(array, endptr); + // CHECK5: {{.*ERROR: AddressSanitizer: heap-buffer-overflow on address}} + // CHECK5: READ of size 4 + if (!strcmp(argv[1], "test6")) test6(array, endptr); + // CHECK6: {{.*ERROR: AddressSanitizer: heap-buffer-overflow on address}} + // CHECK6: READ of size 4 + if (!strcmp(argv[1], "test7")) test7(array, endptr); + // CHECK7: {{.*ERROR: AddressSanitizer: heap-buffer-overflow on address}} + // CHECK7: READ of size 2 + free(array); + return 0; +} |