aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/c-c++-common/analyzer/pr103526.c
blob: ce8fa22a7845b6499c82ad5c0d06b1554ec49481 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/* { dg-skip-if "requires hosted libstdc++ for stdlib malloc" { ! hostedlib } } */

#include <stdlib.h>
#include <string.h>

struct game_state {
	const char *word;
	char       *word_state;
};

const char *const teststr = "test string";

static struct game_state *
game_new(void)
{
	struct game_state tmp = {0};
	struct game_state *rval = NULL;
	size_t wordlen;

	tmp.word = teststr;
	wordlen = strlen(tmp.word);
	if ((tmp.word_state = (char *) malloc(wordlen+1)) == NULL)
		goto err;
	if ((rval = (struct game_state *) malloc(sizeof(*rval))) == NULL)
		goto err;
	memcpy(rval, &tmp, sizeof(*rval));

	return (rval);
err:
	free(tmp.word_state);
	free(rval);
	return (NULL);
} /* { dg-bogus "leak" } */

static void
game_free(struct game_state *game)
{
	if (game == NULL)
		return;
	free(game->word_state);
	free(game);
}

int
main(void)
{
	struct game_state *game;
	if ((game = game_new()) == NULL)
		exit(1);
	game_free(game);
	exit(0);
}