Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 1 | // Test strict_string_checks option in atoi function |
| 2 | // RUN: %clang_asan %s -o %t |
| 3 | // RUN: %run %t test1 2>&1 |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 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 |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 6 | // RUN: %run %t test2 2>&1 |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 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 |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 9 | // RUN: %run %t test3 2>&1 |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 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 |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 12 | |
| 13 | #include <assert.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <string.h> |
| 16 | |
| 17 | void test1(char *array) { |
| 18 | // Last symbol is non-digit |
| 19 | memset(array, '1', 10); |
| 20 | array[9] = 'a'; |
| 21 | int r = atoi(array); |
| 22 | assert(r == 111111111); |
| 23 | } |
| 24 | |
| 25 | void test2(char *array) { |
| 26 | // Single non-digit symbol |
| 27 | array[9] = 'a'; |
| 28 | int r = atoi(array + 9); |
| 29 | assert(r == 0); |
| 30 | } |
| 31 | |
| 32 | void test3(char *array) { |
| 33 | // Incorrect number format |
| 34 | memset(array, ' ', 10); |
| 35 | array[9] = '-'; |
| 36 | array[8] = '-'; |
| 37 | int r = atoi(array); |
| 38 | assert(r == 0); |
| 39 | } |
| 40 | |
| 41 | int main(int argc, char **argv) { |
| 42 | char *array = (char*)malloc(10); |
| 43 | if (argc != 2) return 1; |
| 44 | if (!strcmp(argv[1], "test1")) test1(array); |
| 45 | // CHECK1: {{.*ERROR: AddressSanitizer: heap-buffer-overflow on address}} |
| 46 | // CHECK1: READ of size 11 |
| 47 | if (!strcmp(argv[1], "test2")) test2(array); |
| 48 | // CHECK2: {{.*ERROR: AddressSanitizer: heap-buffer-overflow on address}} |
| 49 | // CHECK2: READ of size 2 |
| 50 | if (!strcmp(argv[1], "test3")) test3(array); |
| 51 | // CHECK3: {{.*ERROR: AddressSanitizer: heap-buffer-overflow on address}} |
| 52 | // CHECK3: READ of size 11 |
| 53 | free(array); |
| 54 | return 0; |
| 55 | } |