| 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 | |
| 12 | static void run(bool flush) { |
| 13 | char *buf; |
| 14 | size_t buf_len; |
| 15 | fprintf(stderr, format: " &buf %p, &buf_len %p\n" , &buf, &buf_len); |
| 16 | FILE *fp = open_memstream(bufloc: &buf, sizeloc: &buf_len); |
| 17 | fprintf(stream: fp, format: "hello" ); |
| 18 | if (flush) { |
| 19 | fflush(stream: 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(s: p, c: 'a', n: 1023); |
| 27 | p[1023] = 0; |
| 28 | for (int i = 0; i < 100; ++i) |
| 29 | fprintf(stream: fp, format: "%s" , p); |
| 30 | delete[] p; |
| 31 | |
| 32 | if (flush) { |
| 33 | fflush(stream: fp); |
| 34 | fprintf(stderr, format: " %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(stream: 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(ptr: buf); |
| 46 | } |
| 47 | |
| 48 | int main(void) { |
| 49 | for (int i = 0; i < 100; ++i) |
| 50 | run(flush: false); |
| 51 | for (int i = 0; i < 100; ++i) |
| 52 | run(flush: true); |
| 53 | return 0; |
| 54 | } |
| 55 | |