2016-10-22 07:52:26 +08:00
|
|
|
// Test strict_string_checks option in strncmp function
|
2016-11-12 12:32:31 +08:00
|
|
|
// RUN: %clang_asan %s -o %t
|
|
|
|
|
|
|
|
// RUN: %env_asan_opts=strict_string_checks=false %run %t a 2>&1
|
|
|
|
// RUN: %env_asan_opts=strict_string_checks=true %run %t a 2>&1
|
|
|
|
// RUN: not %run %t b 2>&1 | FileCheck %s
|
|
|
|
// RUN: not %run %t c 2>&1 | FileCheck %s
|
|
|
|
// RUN: not %run %t d 2>&1 | FileCheck %s
|
|
|
|
// RUN: not %run %t e 2>&1 | FileCheck %s
|
|
|
|
// RUN: not %run %t f 2>&1 | FileCheck %s
|
|
|
|
// RUN: not %run %t g 2>&1 | FileCheck %s
|
|
|
|
// RUN: %env_asan_opts=strict_string_checks=false %run %t h 2>&1
|
|
|
|
// RUN: %env_asan_opts=strict_string_checks=true not %run %t h 2>&1 | FileCheck %s
|
|
|
|
// RUN: %env_asan_opts=strict_string_checks=false %run %t i 2>&1
|
|
|
|
// RUN: %env_asan_opts=strict_string_checks=true not %run %t i 2>&1 | FileCheck %s
|
|
|
|
|
2016-10-22 07:52:26 +08:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
2016-11-12 12:32:31 +08:00
|
|
|
#include <stdio.h>
|
2016-10-22 07:52:26 +08:00
|
|
|
#include <string.h>
|
2016-11-12 12:32:31 +08:00
|
|
|
#include <ctype.h>
|
2016-10-22 07:52:26 +08:00
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2016-11-12 12:32:31 +08:00
|
|
|
assert(argc >= 2);
|
|
|
|
const size_t size = 100;
|
2016-10-22 07:52:26 +08:00
|
|
|
char fill = 'o';
|
2016-11-12 12:32:31 +08:00
|
|
|
char s1[size];
|
|
|
|
char s2[size];
|
2016-10-22 07:52:26 +08:00
|
|
|
memset(s1, fill, size);
|
|
|
|
memset(s2, fill, size);
|
2016-11-12 12:32:31 +08:00
|
|
|
|
2016-11-15 01:51:11 +08:00
|
|
|
switch (argv[1][0]) {
|
2016-11-12 12:32:31 +08:00
|
|
|
case 'a':
|
|
|
|
s1[size - 1] = 'z';
|
|
|
|
s2[size - 1] = 'x';
|
|
|
|
for (int i = 0; i <= size; ++i)
|
2016-11-15 01:51:11 +08:00
|
|
|
assert((strncmp(s1, s2, i) == 0) == (i < size));
|
2016-11-12 12:32:31 +08:00
|
|
|
s1[size - 1] = '\0';
|
|
|
|
s2[size - 1] = '\0';
|
2016-11-15 01:51:11 +08:00
|
|
|
assert(strncmp(s1, s2, 2*size) == 0);
|
2016-11-12 12:32:31 +08:00
|
|
|
break;
|
|
|
|
case 'b':
|
2016-11-15 01:51:11 +08:00
|
|
|
return strncmp(s1-1, s2, 1);
|
2016-11-12 12:32:31 +08:00
|
|
|
case 'c':
|
2016-11-15 01:51:11 +08:00
|
|
|
return strncmp(s1, s2-1, 1);
|
2016-11-12 12:32:31 +08:00
|
|
|
case 'd':
|
2016-11-15 01:51:11 +08:00
|
|
|
return strncmp(s1+size, s2, 1);
|
2016-11-12 12:32:31 +08:00
|
|
|
case 'e':
|
2016-11-15 01:51:11 +08:00
|
|
|
return strncmp(s1, s2+size, 1);
|
2016-11-12 12:32:31 +08:00
|
|
|
case 'f':
|
2016-11-15 01:51:11 +08:00
|
|
|
return strncmp(s1+1, s2, size);
|
2016-11-12 12:32:31 +08:00
|
|
|
case 'g':
|
2016-11-15 01:51:11 +08:00
|
|
|
return strncmp(s1, s2+1, size);
|
2016-11-12 12:32:31 +08:00
|
|
|
case 'h':
|
|
|
|
s1[size - 1] = '\0';
|
2016-11-15 01:51:11 +08:00
|
|
|
assert(strncmp(s1, s2, 2*size) != 0);
|
2016-11-12 12:32:31 +08:00
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
s2[size - 1] = '\0';
|
2016-11-15 01:51:11 +08:00
|
|
|
assert(strncmp(s1, s2, 2*size) != 0);
|
2016-11-12 12:32:31 +08:00
|
|
|
break;
|
|
|
|
// CHECK: {{.*}}ERROR: AddressSanitizer: stack-buffer-{{ov|und}}erflow on address
|
|
|
|
}
|
2016-10-22 07:52:26 +08:00
|
|
|
return 0;
|
|
|
|
}
|