GCC Code Coverage Report


Directory: ./
File: src/nftutils.c
Date: 2026-01-20 23:48:36
Exec Total Coverage
Lines: 13 29 44.8%
Functions: 2 3 66.7%
Branches: 9 26 34.6%

Line Branch Exec Source
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 #include <nft.h>
4
5 #include "nftutils.h"
6
7 #include <netdb.h>
8
9 /* Buffer size used for getprotobynumber_r() and similar. The manual comments
10 * that a buffer of 1024 should be sufficient "for most applications"(??), so
11 * let's double it. It still fits reasonably on the stack, so no need to
12 * choose a smaller one. */
13 #define NETDB_BUFSIZE 2048
14
15 1069 bool nft_getprotobynumber(int proto, char *out_name, size_t name_len)
16 {
17 const struct protoent *result;
18
19 #if HAVE_DECL_GETPROTOBYNUMBER_R
20 struct protoent result_buf;
21 char buf[NETDB_BUFSIZE];
22 int r;
23
24 1069 r = getprotobynumber_r(proto,
25 &result_buf,
26 buf,
27 sizeof(buf),
28 (struct protoent **) &result);
29
2/4
✓ Branch 0 (3→4) taken 1069 times.
✗ Branch 1 (3→5) not taken.
✗ Branch 2 (4→5) not taken.
✓ Branch 3 (4→6) taken 1069 times.
1069 if (r != 0 || result != &result_buf)
30 ✗ result = NULL;
31 #else
32 result = getprotobynumber(proto);
33 #endif
34
35
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 1069 times.
1069 if (!result)
36 ✗ return false;
37
38
1/2
✗ Branch 0 (8→9) not taken.
✓ Branch 1 (8→10) taken 1069 times.
1069 if (strlen(result->p_name) >= name_len)
39 ✗ return false;
40 1069 strcpy(out_name, result->p_name);
41 1069 return true;
42 }
43
44 107 int nft_getprotobyname(const char *name)
45 {
46 const struct protoent *result;
47
48 #if HAVE_DECL_GETPROTOBYNAME_R
49 struct protoent result_buf;
50 char buf[NETDB_BUFSIZE];
51 int r;
52
53 107 r = getprotobyname_r(name,
54 &result_buf,
55 buf,
56 sizeof(buf),
57 (struct protoent **) &result);
58
2/4
✓ Branch 0 (3→4) taken 107 times.
✗ Branch 1 (3→5) not taken.
✗ Branch 2 (4→5) not taken.
✓ Branch 3 (4→6) taken 107 times.
107 if (r != 0 || result != &result_buf)
59 ✗ result = NULL;
60 #else
61 result = getprotobyname(name);
62 #endif
63
64
1/2
✗ Branch 0 (6→7) not taken.
✓ Branch 1 (6→8) taken 107 times.
107 if (!result)
65 ✗ return -1;
66
67
2/4
✓ Branch 0 (8→9) taken 107 times.
✗ Branch 1 (8→10) not taken.
✗ Branch 2 (9→10) not taken.
✓ Branch 3 (9→11) taken 107 times.
107 if (result->p_proto < 0 || result->p_proto > UINT8_MAX)
68 ✗ return -1;
69 107 return (uint8_t) result->p_proto;
70 }
71
72 ✗ bool nft_getservbyport(int port, const char *proto, char *out_name, size_t name_len)
73 {
74 const struct servent *result;
75
76 #if HAVE_DECL_GETSERVBYPORT_R
77 struct servent result_buf;
78 char buf[NETDB_BUFSIZE];
79 int r;
80
81 ✗ r = getservbyport_r(port,
82 proto,
83 &result_buf,
84 buf,
85 sizeof(buf),
86 (struct servent**) &result);
87 ✗ if (r != 0 || result != &result_buf)
88 ✗ result = NULL;
89 #else
90 result = getservbyport(port, proto);
91 #endif
92
93 ✗ if (!result)
94 ✗ return false;
95
96 ✗ if (strlen(result->s_name) >= name_len)
97 ✗ return false;
98 ✗ strcpy(out_name, result->s_name);
99 ✗ return true;
100 }
101