Coverage Report

Created: 2023-06-07 07:02

/src/core/libntech/libutils/alloc.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
  Copyright 2023 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
#include <assert.h> // assert()
31
32
#include <compiler.h>
33
34
void *xcalloc(size_t nmemb, size_t size);
35
void *xmalloc(size_t size);
36
void *xrealloc(void *ptr, size_t size);
37
char *xstrdup(const char *str);
38
char *xstrndup(const char *str, size_t n);
39
void *xmemdup(const void *mem, size_t size);
40
int xasprintf(char **strp, const char *fmt, ...) FUNC_ATTR_PRINTF(2, 3);
41
int xvasprintf(char **strp, const char *fmt, va_list ap) FUNC_ATTR_PRINTF(2, 0);
42
43
static inline void free_array_items(void **array, size_t n_items)
44
0
{
45
0
    assert(array != NULL);
46
0
    for (size_t i = 0; i < n_items; i++)
47
0
    {
48
0
        free(array[i]);
49
0
    }
50
0
}
Unexecuted instantiation: alloc.c:free_array_items
Unexecuted instantiation: cleanup.c:free_array_items
Unexecuted instantiation: misc_lib.c:free_array_items
Unexecuted instantiation: logging.c:free_array_items
Unexecuted instantiation: sequence.c:free_array_items
Unexecuted instantiation: string_lib.c:free_array_items
Unexecuted instantiation: writer.c:free_array_items
51
52
0
#define DESTROY_AND_NULL(destroy, ptr) { destroy(ptr); ptr = NULL; }
53
0
#define FREE_AND_NULL(ptr) { DESTROY_AND_NULL(free, ptr); }
54
55
/*
56
 * Prevent any code from using un-wrapped allocators.
57
 *
58
 * Use x* equivalents instead.
59
 */
60
61
/**
62
 * Currently regular malloc() calls are allowed for mission-critical code that
63
 * can somehow recover, like cf-serverd dropping connections or cf-execd
64
 * postponing its scheduled actions.
65
 *
66
 * @note for 99% of the cases (libpromises, cf-agent etc) use xmalloc() and
67
 *       friends.
68
 **/
69
#if 0
70
71
# undef malloc
72
# undef calloc
73
# undef realloc
74
# undef strdup
75
# undef strndup
76
# undef memdup
77
# undef asprintf
78
# undef vasprintf
79
# define malloc __error_unchecked_malloc
80
# define calloc __error_unchecked_calloc
81
# define realloc __error_unchecked_realloc
82
# define strdup __error_unchecked_strdup
83
# define strndup __error_unchecked_strndup
84
# define memdup __error_unchecked_memdup
85
# define asprintf __error_unchecked_asprintf
86
# define vasprintf __error_unchecked_vasprintf
87
88
void __error_unchecked_malloc(void);
89
void __error_unchecked_calloc(void);
90
void __error_unchecked_realloc(void);
91
void __error_unchecked_strdup(void);
92
void __error_unchecked_strndup(void);
93
void __error_unchecked_memdup(void);
94
void __error_unchecked_asprintf(void);
95
void __error_unchecked_vasprintf(void);
96
97
#endif
98
99
#endif