| 1 | // Test strict_string_checks option in strstr function |
| 2 | // RUN: %clang_asan %s -o %t && %run %t 2>&1 |
| 3 | |
| 4 | // Newer versions of Android's strstr() uses memchr() internally, which actually |
| 5 | // does trigger a heap-buffer-overflow (as it tries to find the |
| 6 | // null-terminator). The same applies to FreeBSD. |
| 7 | // UNSUPPORTED: android, target={{.*freebsd.*}} |
| 8 | // RUN: %env_asan_opts=strict_string_checks=false %run %t 2>&1 |
| 9 | |
| 10 | // RUN: %env_asan_opts=strict_string_checks=true not %run %t 2>&1 | FileCheck %s |
| 11 | |
| 12 | #include <assert.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
| 15 | |
| 16 | int main(int argc, char **argv) { |
| 17 | size_t size = 100; |
| 18 | char fill = 'o'; |
| 19 | char *s1 = (char*)malloc(size: size); |
| 20 | char *s2 = (char*)malloc(size: size); |
| 21 | memset(s: s1, c: fill, n: size); |
| 22 | memset(s: s2, c: fill, n: size); |
| 23 | s2[size - 1]='\0'; |
| 24 | char* r = strstr(haystack: s1, needle: s2); |
| 25 | // CHECK: {{.*ERROR: AddressSanitizer: heap-buffer-overflow on address}} |
| 26 | // CHECK: READ of size {{101|100}} |
| 27 | assert(r == s1); |
| 28 | free(ptr: s1); |
| 29 | free(ptr: s2); |
| 30 | return 0; |
| 31 | } |
| 32 | |