xref: /llvm-project/compiler-rt/test/sanitizer_common/TestCases/Linux/open_memstream.cpp (revision f67fc3acad70c20d8088b72ee9563690c8ab9be0)
1 // RUN: %clangxx -m64 -O0 -g -xc++ %s -o %t && %run %t
2 // RUN: %clangxx -m64 -O3 -g -xc++ %s -o %t && %run %t
3 // REQUIRES: x86_64-target-arch
4 
5 #include <assert.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 
10 #include "sanitizer_common/sanitizer_specific.h"
11 
run(bool flush)12 static void run(bool flush) {
13   char *buf;
14   size_t buf_len;
15   fprintf(stderr, " &buf %p, &buf_len %p\n", &buf, &buf_len);
16   FILE *fp = open_memstream(&buf, &buf_len);
17   fprintf(fp, "hello");
18   if (flush) {
19     fflush(fp);
20     check_mem_is_good(&buf, sizeof(buf));
21     check_mem_is_good(&buf_len, sizeof(buf_len));
22     check_mem_is_good(buf, buf_len);
23   }
24 
25   char *p = new char[1024];
26   memset(p, 'a', 1023);
27   p[1023] = 0;
28   for (int i = 0; i < 100; ++i)
29     fprintf(fp, "%s", p);
30   delete[] p;
31 
32   if (flush) {
33     fflush(fp);
34     fprintf(stderr, " %p addr %p, len %zu\n", &buf, buf, buf_len);
35     check_mem_is_good(&buf, sizeof(buf));
36     check_mem_is_good(&buf_len, sizeof(buf_len));
37     check_mem_is_good(buf, buf_len);\
38   }
39 
40   fclose(fp);
41   check_mem_is_good(&buf, sizeof(buf));
42   check_mem_is_good(&buf_len, sizeof(buf_len));
43   check_mem_is_good(buf, buf_len);
44 
45   free(buf);
46 }
47 
main(void)48 int main(void) {
49   for (int i = 0; i < 100; ++i)
50     run(false);
51   for (int i = 0; i < 100; ++i)
52     run(true);
53   return 0;
54 }
55