models.c 2.3 KB
Newer Older
Mark Andrews's avatar
Mark Andrews committed
1
/*
2
 * Copyright (C) 2015-2017  Internet Systems Consortium, Inc. ("ISC")
Mark Andrews's avatar
Mark Andrews committed
3
 *
4
5
6
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
Mark Andrews's avatar
Mark Andrews committed
7
8
 */

9
10
11
/*
 * Provide a simple memory management model for lib/isc/mem.c
 * which hides all the internal storage and memory filling.
Mark Andrews's avatar
Mark Andrews committed
12
13
 *
 * See https://scan.coverity.com/models
14
15
16
17
18
19
20
21
 */

#define FLARG , const char * file, unsigned int line
#define FLARG_PASS , file, line

int condition;
void *isc__mem_get(void *mem, unsigned int size FLARG) {
	if (!mem) __coverity_panic__();
Mark Andrews's avatar
Mark Andrews committed
22
23
24
25
	__coverity_negative_sink__(size);
	if (condition)
		return (0);
	return (__coverity_alloc__(size));
26
27
28
29
}

void isc__mem_put(void *mem, void *ptr, unsigned int size FLARG) {
	if (!mem) __coverity_panic__();
Mark Andrews's avatar
Mark Andrews committed
30
	__coverity_free__(ptr);
31
32
33
34
}

void isc__mem_putanddetach(void *mem, void *ptr, unsigned int size FLARG) {
	if (!mem) __coverity_panic__();
Mark Andrews's avatar
Mark Andrews committed
35
	__coverity_free__(ptr);
36
37
38
39
}

void *isc__mem_allocate(void *mem, unsigned int size FLARG) {
	if (!mem) __coverity_panic__();
Mark Andrews's avatar
Mark Andrews committed
40
41
42
43
	__coverity_negative_sink__(size);
	if (condition)
		return (0);
	return (__coverity_alloc__(size));
44
45
46
47
48
}

void *memcpy(void *s1, const void *s2, size_t n);

void * isc__mem_reallocate(void *mem, void *ptr, size_t size FLARG) {
Mark Andrews's avatar
Mark Andrews committed
49
50
	char *p = (char *)0;
	size_t l;
51

Mark Andrews's avatar
Mark Andrews committed
52
53
54
55
	if (!mem) __coverity_panic__();
	if (size > 0) {
		p = isc__mem_allocate(mem, size FLARG_PASS);
		if (p && ptr) {
56
			l = (l > size) ? size : l;
Mark Andrews's avatar
Mark Andrews committed
57
58
59
60
61
62
			memcpy(p, ptr, l);
			__coverity_free__(ptr);
		}
	} else if (ptr)
		__coverity_free__(ptr);
	return (p);
63
64
65
66
}

void isc__mem_free(void *mem, void *ptr FLARG) {
	if (!mem) __coverity_panic__();
Mark Andrews's avatar
Mark Andrews committed
67
	__coverity_free__(ptr);
68
69
70
71
72
}

unsigned int strlen(const char*);

void *isc__mem_strdup(void *mem, char *s FLARG) {
Mark Andrews's avatar
Mark Andrews committed
73
	void *d;
74
	if (!mem) __coverity_panic__();
Mark Andrews's avatar
Mark Andrews committed
75
76
77
78
79
	if (condition)
		return (0);
	d = __coverity_alloc__(strlen(s) + 1);
	__coverity_writeall__(d);
	return (d);
80
81
82
}

void *isc__mempool_get(void *mem FLARG) {
Mark Andrews's avatar
Mark Andrews committed
83
	unsigned int size;
84
	if (!mem) __coverity_panic__();
Mark Andrews's avatar
Mark Andrews committed
85
86
87
	if (condition)
		return (0);
	return (__coverity_alloc__(size));
88
89
90
91
}

void isc__mempool_put(void *mem, void *ptr FLARG) {
	if (!mem) __coverity_panic__();
Mark Andrews's avatar
Mark Andrews committed
92
	__coverity_free__(ptr);
93
}
94
95
96
97

void atf_tc_fail_requirement(const char *, const size_t, const char *, ...) {
	__coverity_panic__();
}