xref: /llvm-project/compiler-rt/test/sanitizer_common/TestCases/vsnprintf.cpp (revision 94b0851aad618397f508e56c63c6f928e4e911b9)
1 // Test that the common part implementation of *printf interceptors does not
2 // cause negative-size-param false positives.
3 
4 // RUN: %clangxx -O2 %s -o %t
5 // RUN: %env_tool_opts=check_printf=1 %run %t 2>&1
6 
7 // FIXME: The maximum supported allocation size is too platform-specific:
8 // REQUIRES: x86_64-target-arch
9 
10 // FIXME: printf is not intercepted on Windows yet.
11 // UNSUPPORTED: target={{.*windows-msvc.*}}
12 
13 #include <stdarg.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 
write(char * buf,int buf_size,const char * fmt,...)18 void write(char *buf, int buf_size, const char *fmt, ...) {
19   va_list args;
20   va_start(args, fmt);
21   vsnprintf(buf, buf_size, fmt, args);
22   va_end(args);
23 }
24 
main()25 int main() {
26   char buffer[100];
27   const size_t kStrSize = 1UL << 31;
28   char *x = (char *)malloc(kStrSize);
29   memset(x, '=', kStrSize - 1);
30   x[kStrSize - 1] = 0;
31   write(buffer, 100, "%s\n", x);
32   free(x);
33   return 0;
34 }
35