Add __builtion_unreachable to vector::size(), vector::capacity()
[official-gcc.git] / gcc / testsuite / c-c++-common / analyzer / leak-pr108045-with-call-summaries.c
blobe4be5093641b4421efea38ecd045dd55b91bf15c
1 /* { dg-additional-options "-fanalyzer-call-summaries" } */
3 typedef __SIZE_TYPE__ size_t;
4 #include "../../gcc.dg/analyzer/analyzer-decls.h"
6 /* data structures */
8 struct screen_s {
9 size_t rows;
10 size_t cols;
11 char **data;
14 struct context_s {
15 struct screen_s *scr;
18 /* global context variable */
20 static struct context_s *ctx;
22 /* prototypes */
24 struct screen_s *screen_create(size_t cols, size_t rows);
25 void screen_destroy(struct screen_s *scr);
26 void resize_screen(size_t cols, size_t rows);
28 /* functions */
30 struct screen_s *screen_create(size_t cols, size_t rows)
32 struct screen_s *result = NULL;
34 result = (struct screen_s *) __builtin_calloc(1, sizeof(*result));
35 if (!result)
36 return NULL;
38 result->cols = cols;
39 result->rows = rows;
41 /* make one allocation which will be accessed like a 2D array */
42 result->data = (char **) __builtin_calloc(rows, sizeof(result->data) + sizeof(*result->data) * cols);
43 if (!result->data) {
44 __builtin_free(result);
45 return NULL;
48 /* obtain pointer to start of data area */
49 char *ptr = (char *)(result->data + rows);
51 /* setup pointers for each row of data to allow 2D array access */
52 for (size_t row = 0; row < rows; row++)
53 result->data[row] = (ptr + row * cols);
54 /* array can now be accessed like data[row][col] */
56 return result;
59 void screen_destroy(struct screen_s *scr)
61 if (!scr)
62 return;
64 __builtin_free(scr->data);
66 scr->data = NULL;
67 scr->rows = 0;
68 scr->cols = 0;
70 __builtin_free(scr);
73 void resize_screen(size_t cols, size_t rows)
75 /* create a new screen */
76 struct screen_s *new_scr = NULL;
77 new_scr = screen_create(cols, rows); /* { dg-bogus "leak" "PR 108045" { xfail *-*-* } } */
78 if (!new_scr) {
79 return;
82 /* swap the old screen with the new one */
83 struct screen_s *old_scr = ctx->scr;
84 ctx->scr = new_scr;
86 /* omitted: copy the old screen contents to the new screen */
88 /* free the old screen */
89 screen_destroy(old_scr);
92 int main(void)
94 ctx = (struct context_s *) __builtin_calloc(1, sizeof(*ctx));
95 if (!ctx)
96 __builtin_abort();
98 ctx->scr = screen_create(80, 25); /* { dg-bogus "leak" "PR 108045" { xfail *-*-* } } */
99 resize_screen(100, 20);
101 /* tidy up and quit */
102 screen_destroy(ctx->scr);
103 __builtin_free(ctx);
104 ctx = NULL;
105 return 0;