Debian Codegraph @

gcc/testsuite/c-c++-common/analyzer/pr103526.c

raw
1 /* { dg-skip-if "requires hosted libstdc++ for stdlib malloc" { ! hostedlib } } */
2
3 #include <stdlib.h>
4 #include <string.h>
5
6 struct game_state {
7 const char *word;
8 char *word_state;
9 };
10
11 const char *const teststr = "test string";
12
13 static struct game_state *
14 game_new(void)
15 {
16 struct game_state tmp = {0};
17 struct game_state *rval = NULL;
18 size_t wordlen;
19
20 tmp.word = teststr;
21 wordlen = strlen(tmp.word);
22 if ((tmp.word_state = (char *) malloc(wordlen+1)) == NULL)
23 goto err;
24 if ((rval = (struct game_state *) malloc(sizeof(*rval))) == NULL)
25 goto err;
26 memcpy(rval, &tmp, sizeof(*rval));
27
28 return (rval);
29 err:
30 free(tmp.word_state);
31 free(rval);
32 return (NULL);
33 } /* { dg-bogus "leak" } */
34
35 static void
36 game_free(struct game_state *game)
37 {
38 if (game == NULL)
39 return;
40 free(game->word_state);
41 free(game);
42 }
43
44 int
45 main(void)
46 {
47 struct game_state *game;
48 if ((game = game_new()) == NULL)
49 exit(1);
50 game_free(game);
51 exit(0);
52 }