GCC Code Coverage Report


Directory: ./
File: src/utils.c
Date: 2026-01-20 23:48:36
Exec Total Coverage
Lines: 34 40 85.0%
Functions: 7 8 87.5%
Branches: 11 20 55.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
10
11 #include <nft.h>
12
13 #include <stddef.h>
14 #include <stdarg.h>
15 #include <stdio.h>
16 #include <unistd.h>
17
18 #include <nftables.h>
19 #include <utils.h>
20
21 − void __noreturn __memory_allocation_error(const char *filename, uint32_t line)
22 {
23 − fprintf(stderr, "%s:%u: Memory allocation failure\n", filename, line);
24 − exit(NFT_EXIT_NOMEM);
25 }
26
27 29799497 void *xmalloc(size_t size)
28 {
29 void *ptr;
30
31 29799497 ptr = malloc(size);
32
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 29799497 times.
29799497 if (ptr == NULL)
33 ✗ memory_allocation_error();
34 29799497 return ptr;
35 }
36
37 247141 void *xmalloc_array(size_t nmemb, size_t size)
38 {
39
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 247141 times.
247141 assert(size != 0);
40
1/2
✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 247141 times.
247141 assert(nmemb != 0);
41
42
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 247141 times.
247141 if (nmemb > SIZE_MAX / size)
43 ✗ memory_allocation_error();
44
45 247141 return xmalloc(nmemb * size);
46 }
47
48 238877 void *xzalloc_array(size_t nmemb, size_t size)
49 {
50 void *ptr;
51
52 238877 ptr = xmalloc_array(nmemb, size);
53 238877 memset(ptr, 0, nmemb * size);
54
55 238877 return ptr;
56 }
57
58 3274 void *xrealloc(void *ptr, size_t size)
59 {
60 3274 ptr = realloc(ptr, size);
61
1/4
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→5) taken 3274 times.
✗ Branch 2 (3→4) not taken.
✗ Branch 3 (3→5) not taken.
3274 if (ptr == NULL && size != 0)
62 ✗ memory_allocation_error();
63 3274 return ptr;
64 }
65
66 28703914 void *xzalloc(size_t size)
67 {
68 void *ptr;
69
70 28703914 ptr = xmalloc(size);
71 28703914 memset(ptr, 0, size);
72 28703914 return ptr;
73 }
74
75 4526669 char *xstrdup(const char *s)
76 {
77 char *res;
78
79
1/2
✗ Branch 0 (2→3) not taken.
✓ Branch 1 (2→4) taken 4526669 times.
4526669 assert(s != NULL);
80 4526669 res = strdup(s);
81
1/2
✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 4526669 times.
4526669 if (res == NULL)
82 ✗ memory_allocation_error();
83 4526669 return res;
84 }
85
86 42 void xstrunescape(const char *in, char *out)
87 {
88 42 unsigned int i, k = 0;
89
90
2/2
✓ Branch 0 (7→3) taken 294 times.
✓ Branch 1 (7→8) taken 42 times.
336 for (i = 0; i < strlen(in); i++) {
91
2/2
✓ Branch 0 (3→4) taken 42 times.
✓ Branch 1 (3→5) taken 252 times.
294 if (in[i] == '\\')
92 42 continue;
93
94 252 out[k++] = in[i];
95 }
96 42 out[k++] = '\0';
97 42 }
98
99 ✗ int round_pow_2(unsigned int n)
100 {
101 ✗ return 1UL << fls(n - 1);
102 }
103