1// RUN: %clangxx_asan -O0 %s -o %t
2// RUN: %env_asan_opts=allocator_may_return_null=0 not %run %t 2>&1 | FileCheck %s
3// RUN: %env_asan_opts=allocator_may_return_null=1 %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-NULL
4
5// REQUIRES: stable-runtime
6
7#include <stdio.h>
8#include <stdlib.h>
9
10int main() {
11 void *p = calloc(nmemb: -1, size: 1000);
12 // CHECK: {{ERROR: AddressSanitizer: calloc parameters overflow: count \* size \(.* \* 1000\) cannot be represented in type size_t}}
13 // CHECK: {{#0 0x.* in .*calloc}}
14 // CHECK: {{#1 0x.* in main .*calloc-overflow.cpp:}}[[@LINE-3]]
15 // CHECK: SUMMARY: AddressSanitizer: calloc-overflow
16
17 printf(format: "calloc returned: %zu\n", (size_t)p);
18 // CHECK-NULL: calloc returned: 0
19
20 return 0;
21}
22

source code of compiler-rt/test/asan/TestCases/calloc-overflow.cpp