aboutsummaryrefslogtreecommitdiffstats
path: root/libautomata_grow_automaton.c
blob: 98ce12fa00a9421c45eb8a190ddeb6a74bf4758c (plain) (blame)
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
/* See LICENSE file for copyright and license details. */
#include "common.h"


int
libautomata_grow_automaton(struct libautomata_automaton *automaton, size_t n)
{
	struct libautomata_node *new;
	size_t size;

	if (automaton->nnodes > SIZE_MAX - n)
		goto enomem;
	size = automaton->nnodes + n;
	if (size > SIZE_MAX / sizeof(*automaton->nodes))
		goto enomem;
	size *= sizeof(*automaton->nodes);

	new = realloc(automaton->nodes, size);
	if (!new) {
	enomem:
		errno = ENOMEM;
		return -1;
	}
	automaton->nodes = new;

	return 0;
}