36 lines · cpp
1// RUN: %clang_asan -Wno-alloc-size -fsanitize-recover=address %s -o %t && %env_asan_opts=halt_on_error=0 %run %t 2>&1 | FileCheck %s2 3#include <stdio.h>4#include <stdlib.h>5 6int main(int argc, char **argv) {7 {8 char *p1 = (char *)calloc(1, 0);9 printf("p1 is %p\n", p1);10 printf("Content of p1 is: %d\n", *p1);11 // CHECK: ERROR: AddressSanitizer: heap-buffer-overflow12 // CHECK: {{#0 0x.* in main .*zero_alloc.cpp:}}[[@LINE-2]]13 free(p1);14 }15 16 {17 char *p2 = (char *)calloc(0, 1);18 printf("p2 is %p\n", p2);19 printf("Content of p2 is: %d\n", *p2);20 // CHECK: ERROR: AddressSanitizer: heap-buffer-overflow21 // CHECK: {{#0 0x.* in main .*zero_alloc.cpp:}}[[@LINE-2]]22 free(p2);23 }24 25 {26 char *p3 = (char *)malloc(0);27 printf("p3 is %p\n", p3);28 printf("Content of p2 is: %d\n", *p3);29 // CHECK: ERROR: AddressSanitizer: heap-buffer-overflow30 // CHECK: {{#0 0x.* in main .*zero_alloc.cpp:}}[[@LINE-2]]31 free(p3);32 }33 34 return 0;35}36