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