summaryrefslogtreecommitdiff
path: root/src/network/networkd-dhcp-server.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2021-05-18 23:01:32 +0200
committerLennart Poettering <lennart@poettering.net>2021-05-19 16:42:37 +0200
commit319a4f4bc46b230fc660321e99aaac1bc449deea (patch)
tree9eca2e1352df29aeeef91e4fd4bcc12ad4ea44e9 /src/network/networkd-dhcp-server.c
parent99480504d47c0a6bbb1ac230cc33df12bbd36c54 (diff)
downloadsystemd-319a4f4bc46b230fc660321e99aaac1bc449deea.tar.gz
alloc-util: simplify GREEDY_REALLOC() logic by relying on malloc_usable_size()
We recently started making more use of malloc_usable_size() and rely on it (see the string_erase() story). Given that we don't really support sytems where malloc_usable_size() cannot be trusted beyond statistics anyway, let's go fully in and rework GREEDY_REALLOC() on top of it: instead of passing around and maintaining the currenly allocated size everywhere, let's just derive it automatically from malloc_usable_size(). I am mostly after this for the simplicity this brings. It also brings minor efficiency improvements I guess, but things become so much nicer to look at if we can avoid these allocation size variables everywhere. Note that the malloc_usable_size() man page says relying on it wasn't "good programming practice", but I think it does this for reasons that don't apply here: the greedy realloc logic specifically doesn't rely on the returned extra size, beyond the fact that it is equal or larger than what was requested. (This commit was supposed to be a quick patch btw, but apparently we use the greedy realloc stuff quite a bit across the codebase, so this ends up touching *a*lot* of code.)
Diffstat (limited to 'src/network/networkd-dhcp-server.c')
-rw-r--r--src/network/networkd-dhcp-server.c20
1 files changed, 12 insertions, 8 deletions
diff --git a/src/network/networkd-dhcp-server.c b/src/network/networkd-dhcp-server.c
index 0a396a957f..36707b6e8f 100644
--- a/src/network/networkd-dhcp-server.c
+++ b/src/network/networkd-dhcp-server.c
@@ -101,8 +101,8 @@ static int link_push_uplink_to_dhcp_server(
sd_dhcp_server *s) {
_cleanup_free_ struct in_addr *addresses = NULL;
- size_t n_addresses = 0, n_allocated = 0;
bool use_dhcp_lease_data = true;
+ size_t n_addresses = 0;
assert(link);
@@ -131,7 +131,7 @@ static int link_push_uplink_to_dhcp_server(
if (in4_addr_is_null(&ia) || in4_addr_is_localhost(&ia))
continue;
- if (!GREEDY_REALLOC(addresses, n_allocated, n_addresses + 1))
+ if (!GREEDY_REALLOC(addresses, n_addresses + 1))
return log_oom();
addresses[n_addresses++] = ia;
@@ -156,7 +156,7 @@ static int link_push_uplink_to_dhcp_server(
if (in4_addr_is_null(&ia.in) || in4_addr_is_localhost(&ia.in))
continue;
- if (!GREEDY_REALLOC(addresses, n_allocated, n_addresses + 1))
+ if (!GREEDY_REALLOC(addresses, n_addresses + 1))
return log_oom();
addresses[n_addresses++] = ia.in;
@@ -188,7 +188,7 @@ static int link_push_uplink_to_dhcp_server(
int n = sd_dhcp_lease_get_servers(link->dhcp_lease, what, &da);
if (n > 0) {
- if (!GREEDY_REALLOC(addresses, n_allocated, n_addresses + n))
+ if (!GREEDY_REALLOC(addresses, n_addresses + n))
return log_oom();
for (int j = 0; j < n; j++)
@@ -203,7 +203,11 @@ static int link_push_uplink_to_dhcp_server(
return sd_dhcp_server_set_servers(s, what, addresses, n_addresses);
}
-static int dhcp4_server_parse_dns_server_string_and_warn(Link *l, const char *string, struct in_addr **addresses, size_t *n_allocated, size_t *n_addresses) {
+static int dhcp4_server_parse_dns_server_string_and_warn(
+ const char *string,
+ struct in_addr **addresses,
+ size_t *n_addresses) {
+
for (;;) {
_cleanup_free_ char *word = NULL, *server_name = NULL;
union in_addr_union address;
@@ -229,7 +233,7 @@ static int dhcp4_server_parse_dns_server_string_and_warn(Link *l, const char *st
if (in4_addr_is_null(&address.in) || in4_addr_is_localhost(&address.in))
continue;
- if (!GREEDY_REALLOC(*addresses, *n_allocated, *n_addresses + 1))
+ if (!GREEDY_REALLOC(*addresses, *n_addresses + 1))
return log_oom();
(*addresses)[(*n_addresses)++] = address.in;
@@ -240,8 +244,8 @@ static int dhcp4_server_parse_dns_server_string_and_warn(Link *l, const char *st
static int dhcp4_server_set_dns_from_resolve_conf(Link *link) {
_cleanup_free_ struct in_addr *addresses = NULL;
- size_t n_addresses = 0, n_allocated = 0;
_cleanup_fclose_ FILE *f = NULL;
+ size_t n_addresses = 0;
int n = 0, r;
f = fopen(PRIVATE_UPLINK_RESOLV_CONF, "re");
@@ -273,7 +277,7 @@ static int dhcp4_server_set_dns_from_resolve_conf(Link *link) {
if (!a)
continue;
- r = dhcp4_server_parse_dns_server_string_and_warn(link, a, &addresses, &n_allocated, &n_addresses);
+ r = dhcp4_server_parse_dns_server_string_and_warn(a, &addresses, &n_addresses);
if (r < 0)
log_warning_errno(r, "Failed to parse DNS server address '%s', ignoring.", a);
}