27 lines · cpp
1// RUN: %clangxx_asan -O0 %s -o %t && not %env_asan_opts=log_to_stderr=1 %run %t 2>&1 | FileCheck %s --check-prefix=CHECK2// RUN: %clangxx_asan -O1 %s -o %t && not %env_asan_opts=log_to_stderr=1 %run %t 2>&1 | FileCheck %s --check-prefix=CHECK3// RUN: %clangxx_asan -O2 %s -o %t && not %env_asan_opts=log_to_stderr=1 %run %t 2>&1 | FileCheck %s --check-prefix=CHECK4// RUN: %clangxx_asan -O3 %s -o %t && not %env_asan_opts=log_to_stderr=1 %run %t 2>&1 | FileCheck %s --check-prefix=CHECK5 6#include <stdio.h>7#include <wchar.h>8 9int main() {10 const wchar_t *src = L"X means dog";11 wchar_t goodArray[12];12 wchar_t *volatile goodDst = goodArray;13 wcsncpy(goodDst, src, 12);14 15 wchar_t badArray[7];16 wchar_t *volatile badDst = badArray;17 wcsncpy(badDst, src, 7); // This should still work.18 fprintf(stderr, "Good so far.\n");19 // CHECK-DAG: Good so far.20 fflush(stderr);21 wcsncpy(badDst, src, 15); // Boom!22 // CHECK-DAG: ERROR: AddressSanitizer: stack-buffer-overflow on address [[ADDR:0x[0-9a-f]+]] at pc {{0x[0-9a-f]+}} bp {{0x[0-9a-f]+}} sp {{0x[0-9a-f]+}}23 // CHECK-DAG: WRITE of size {{[0-9]+}} at [[ADDR]] thread T024 // CHECK-DAG: #0 {{0x[0-9a-f]+}} in wcsncpy25 printf("Should have failed with ASAN error.\n");26}27