1 // Test strict_string_checks option in atoll function
2 // RUN: %clang_asan %s -o %t
3 // RUN: %run %t test1 2>&1
4 // RUN: %env_asan_opts=strict_string_checks=false %run %t test1 2>&1
5 // RUN: %env_asan_opts=strict_string_checks=true not %run %t test1 2>&1 | FileCheck %s --check-prefix=CHECK1
6 // RUN: %run %t test2 2>&1
7 // RUN: %env_asan_opts=strict_string_checks=false %run %t test2 2>&1
8 // RUN: %env_asan_opts=strict_string_checks=true not %run %t test2 2>&1 | FileCheck %s --check-prefix=CHECK2
9 // RUN: %run %t test3 2>&1
10 // RUN: %env_asan_opts=strict_string_checks=false %run %t test3 2>&1
11 // RUN: %env_asan_opts=strict_string_checks=true not %run %t test3 2>&1 | FileCheck %s --check-prefix=CHECK3
12
13 // FIXME: Needs Windows interceptor.
14 // XFAIL: win32
15
16 #include <assert.h>
17 #include <stdlib.h>
18 #include <string.h>
19
test1(char * array)20 void test1(char *array) {
21 // Last symbol is non-digit
22 memset(array, '1', 10);
23 array[9] = 'a';
24 long long r = atoll(array);
25 assert(r == 111111111);
26 }
27
test2(char * array)28 void test2(char *array) {
29 // Single non-digit symbol
30 array[9] = 'a';
31 long long r = atoll(array + 9);
32 assert(r == 0);
33 }
34
test3(char * array)35 void test3(char *array) {
36 // Incorrect number format
37 memset(array, ' ', 10);
38 array[9] = '-';
39 array[8] = '-';
40 long long r = atoll(array);
41 assert(r == 0);
42 }
43
main(int argc,char ** argv)44 int main(int argc, char **argv) {
45 char *array = (char*)malloc(10);
46 if (argc != 2) return 1;
47 if (!strcmp(argv[1], "test1")) test1(array);
48 // CHECK1: {{.*ERROR: AddressSanitizer: heap-buffer-overflow on address}}
49 // CHECK1: READ of size 11
50 if (!strcmp(argv[1], "test2")) test2(array);
51 // CHECK2: {{.*ERROR: AddressSanitizer: heap-buffer-overflow on address}}
52 // CHECK2: READ of size 2
53 if (!strcmp(argv[1], "test3")) test3(array);
54 // CHECK3: {{.*ERROR: AddressSanitizer: heap-buffer-overflow on address}}
55 // CHECK3: READ of size 11
56 free(array);
57 return 0;
58 }
59