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// XFAIL: target={{.*windows-(msvc.*|gnu)}}
18
19#include <assert.h>
20#include <stdlib.h>
21#include <stdio.h>
22#include <string.h>
23#include <ctype.h>
24
25int main(int argc, char **argv) {
26 assert(argc >= 2);
27 enum { size = 100 };
28 char fill = 'o';
29 char s1[size];
30 char s2[size];
31 memset(s: s1, c: fill, n: size);
32 memset(s: s2, c: fill, n: size);
33
34 switch (argv[1][0]) {
35 case 'a':
36 s1[size - 1] = 'z';
37 s2[size - 1] = 'x';
38 for (int i = 0; i <= size; ++i)
39 assert((strncasecmp(s1, s2, i) == 0) == (i < size));
40 s1[size - 1] = '\0';
41 s2[size - 1] = '\0';
42 assert(strncasecmp(s1, s2, 2*size) == 0);
43 break;
44 case 'b':
45 return strncasecmp(s1: s1-1, s2: s2, n: 1);
46 case 'c':
47 return strncasecmp(s1: s1, s2: s2-1, n: 1);
48 case 'd':
49 return strncasecmp(s1: s1+size, s2: s2, n: 1);
50 case 'e':
51 return strncasecmp(s1: s1, s2: s2+size, n: 1);
52 case 'f':
53 return strncasecmp(s1: s1+1, s2: s2, n: size);
54 case 'g':
55 return strncasecmp(s1: s1, s2: s2+1, n: size);
56 case 'h':
57 s1[size - 1] = '\0';
58 assert(strncasecmp(s1, s2, 2*size) != 0);
59 break;
60 case 'i':
61 s2[size - 1] = '\0';
62 assert(strncasecmp(s1, s2, 2*size) != 0);
63 break;
64 // CHECK: {{.*}}ERROR: AddressSanitizer: stack-buffer-{{ov|und}}erflow on address
65 }
66 return 0;
67}
68

source code of compiler-rt/test/asan/TestCases/strncasecmp_strict.c