68 lines · c
1// Test strict_string_checks option in strncmp function2// RUN: %clang_asan %s -o %t3 4// RUN: %env_asan_opts=strict_string_checks=false %run %t a 2>&15// RUN: %env_asan_opts=strict_string_checks=true %run %t a 2>&16// RUN: not %run %t b 2>&1 | FileCheck %s7// RUN: not %run %t c 2>&1 | FileCheck %s8// RUN: not %run %t d 2>&1 | FileCheck %s9// RUN: not %run %t e 2>&1 | FileCheck %s10// RUN: not %run %t f 2>&1 | FileCheck %s11// RUN: not %run %t g 2>&1 | FileCheck %s12// RUN: %env_asan_opts=strict_string_checks=false %run %t h 2>&113// RUN: %env_asan_opts=strict_string_checks=true not %run %t h 2>&1 | FileCheck %s14// RUN: %env_asan_opts=strict_string_checks=false %run %t i 2>&115// RUN: %env_asan_opts=strict_string_checks=true not %run %t i 2>&1 | FileCheck %s16 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(s1, fill, size);32 memset(s2, fill, 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-1, s2, 1);46 case 'c':47 return strncasecmp(s1, s2-1, 1);48 case 'd':49 return strncasecmp(s1+size, s2, 1);50 case 'e':51 return strncasecmp(s1, s2+size, 1);52 case 'f':53 return strncasecmp(s1+1, s2, size);54 case 'g':55 return strncasecmp(s1, s2+1, 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 address65 }66 return 0;67}68