1 | // Test strict_string_checks option in strncmp function |
2 | // RUN: %clang_asan %s -o %t |
3 | |
4 | // RUN: %env_asan_opts=strict_string_checks=false %run %t a 2>&1 |
5 | // RUN: %env_asan_opts=strict_string_checks=true %run %t a 2>&1 |
6 | // RUN: not %run %t b 2>&1 | FileCheck %s |
7 | // RUN: not %run %t c 2>&1 | FileCheck %s |
8 | // RUN: not %run %t d 2>&1 | FileCheck %s |
9 | // RUN: not %run %t e 2>&1 | FileCheck %s |
10 | // RUN: not %run %t f 2>&1 | FileCheck %s |
11 | // RUN: not %run %t g 2>&1 | FileCheck %s |
12 | // RUN: %env_asan_opts=strict_string_checks=false %run %t h 2>&1 |
13 | // RUN: %env_asan_opts=strict_string_checks=true not %run %t h 2>&1 | FileCheck %s |
14 | // RUN: %env_asan_opts=strict_string_checks=false %run %t i 2>&1 |
15 | // RUN: %env_asan_opts=strict_string_checks=true not %run %t i 2>&1 | FileCheck %s |
16 | |
17 | #include <assert.h> |
18 | #include <stdlib.h> |
19 | #include <stdio.h> |
20 | #include <string.h> |
21 | #include <ctype.h> |
22 | |
23 | int main(int argc, char **argv) { |
24 | assert(argc >= 2); |
25 | enum { size = 100 }; |
26 | char fill = 'o'; |
27 | char s1[size]; |
28 | char s2[size]; |
29 | memset(s: s1, c: fill, n: size); |
30 | memset(s: s2, c: fill, n: size); |
31 | |
32 | switch (argv[1][0]) { |
33 | case 'a': |
34 | s1[size - 1] = 'z'; |
35 | s2[size - 1] = 'x'; |
36 | for (int i = 0; i <= size; ++i) |
37 | assert((strncmp(s1, s2, i) == 0) == (i < size)); |
38 | s1[size - 1] = '\0'; |
39 | s2[size - 1] = '\0'; |
40 | assert(strncmp(s1, s2, 2*size) == 0); |
41 | break; |
42 | case 'b': |
43 | return strncmp(s1: s1-1, s2: s2, n: 1); |
44 | case 'c': |
45 | return strncmp(s1: s1, s2: s2-1, n: 1); |
46 | case 'd': |
47 | return strncmp(s1: s1+size, s2: s2, n: 1); |
48 | case 'e': |
49 | return strncmp(s1: s1, s2: s2+size, n: 1); |
50 | case 'f': |
51 | return strncmp(s1: s1+1, s2: s2, n: size); |
52 | case 'g': |
53 | return strncmp(s1: s1, s2: s2+1, n: size); |
54 | case 'h': |
55 | s1[size - 1] = '\0'; |
56 | assert(strncmp(s1, s2, 2*size) != 0); |
57 | break; |
58 | case 'i': |
59 | s2[size - 1] = '\0'; |
60 | assert(strncmp(s1, s2, 2*size) != 0); |
61 | break; |
62 | // CHECK: {{.*}}ERROR: AddressSanitizer: stack-buffer-{{ov|und}}erflow on address |
63 | } |
64 | return 0; |
65 | } |
66 | |