1 /*
2   Copyright 2020 Northern.tech AS
3 
4   This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
5 
6   This program is free software; you can redistribute it and/or modify it
7   under the terms of the GNU General Public License as published by the
8   Free Software Foundation; version 3.
9 
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14 
15   You should have received a copy of the GNU General Public License
16   along with this program; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
18 
19   To the extent this program is licensed as part of the Enterprise
20   versions of CFEngine, the applicable Commercial Open Source License
21   (COSL) may apply to this file if you as a licensee so wish it. See
22   included file COSL.txt.
23 */
24 
25 #ifndef CFENGINE_ALLOC_H
26 #define CFENGINE_ALLOC_H
27 
28 #include <stdlib.h> // size_t
29 #include <stdarg.h> // va_list
30 
31 #include <compiler.h>
32 
33 void *xcalloc(size_t nmemb, size_t size);
34 void *xmalloc(size_t size);
35 void *xrealloc(void *ptr, size_t size);
36 char *xstrdup(const char *str);
37 char *xstrndup(const char *str, size_t n);
38 void *xmemdup(const void *mem, size_t size);
39 int xasprintf(char **strp, const char *fmt, ...) FUNC_ATTR_PRINTF(2, 3);
40 int xvasprintf(char **strp, const char *fmt, va_list ap) FUNC_ATTR_PRINTF(2, 0);
41 
42 #define DESTROY_AND_NULL(destroy, ptr) { destroy(ptr); ptr = NULL; }
43 #define FREE_AND_NULL(ptr) { DESTROY_AND_NULL(free, ptr); }
44 
45 /*
46  * Prevent any code from using un-wrapped allocators.
47  *
48  * Use x* equivalents instead.
49  */
50 
51 /**
52  * Currently regular malloc() calls are allowed for mission-critical code that
53  * can somehow recover, like cf-serverd dropping connections or cf-execd
54  * postponing its scheduled actions.
55  *
56  * @note for 99% of the cases (libpromises, cf-agent etc) use xmalloc() and
57  *       friends.
58  **/
59 #if 0
60 
61 # undef malloc
62 # undef calloc
63 # undef realloc
64 # undef strdup
65 # undef strndup
66 # undef memdup
67 # undef asprintf
68 # undef vasprintf
69 # define malloc __error_unchecked_malloc
70 # define calloc __error_unchecked_calloc
71 # define realloc __error_unchecked_realloc
72 # define strdup __error_unchecked_strdup
73 # define strndup __error_unchecked_strndup
74 # define memdup __error_unchecked_memdup
75 # define asprintf __error_unchecked_asprintf
76 # define vasprintf __error_unchecked_vasprintf
77 
78 void __error_unchecked_malloc(void);
79 void __error_unchecked_calloc(void);
80 void __error_unchecked_realloc(void);
81 void __error_unchecked_strdup(void);
82 void __error_unchecked_strndup(void);
83 void __error_unchecked_memdup(void);
84 void __error_unchecked_asprintf(void);
85 void __error_unchecked_vasprintf(void);
86 
87 #endif
88 
89 #endif
90