From: Zbigniew Jędrzejewski-Szmek Date: Mon, 6 Jul 2020 14:21:34 +0000 (+0200) Subject: networkd: we should not loop when extract_first_word() fails X-Git-Tag: v246-rc1~30^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=1f68f77292a9581dd77be9a0bd279c1430cbd130;p=thirdparty%2Fsystemd.git networkd: we should not loop when extract_first_word() fails While at it, define iterator in the loop to reduce the indentation a bit. --- diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c index 4df56087198..053aa3b4cee 100644 --- a/src/network/networkd-link.c +++ b/src/network/networkd-link.c @@ -3450,7 +3450,6 @@ static int link_load(Link *link) { *dhcp4_address = NULL, *ipv4ll_address = NULL; union in_addr_union address; - const char *p; int r; assert(link); @@ -3489,107 +3488,100 @@ static int link_load(Link *link) { network_file_fail: - if (addresses) { - p = addresses; + for (const char *p = addresses; p; ) { + _cleanup_free_ char *address_str = NULL; + char *prefixlen_str; + int family; + unsigned char prefixlen; - for (;;) { - _cleanup_free_ char *address_str = NULL; - char *prefixlen_str; - int family; - unsigned char prefixlen; - - r = extract_first_word(&p, &address_str, NULL, 0); - if (r < 0) { - log_link_debug_errno(link, r, "Failed to extract next address string: %m"); - continue; - } - if (r == 0) - break; - - prefixlen_str = strchr(address_str, '/'); - if (!prefixlen_str) { - log_link_debug(link, "Failed to parse address and prefix length %s", address_str); - continue; - } - - *prefixlen_str++ = '\0'; + r = extract_first_word(&p, &address_str, NULL, 0); + if (r < 0) + log_link_warning_errno(link, r, "failed to parse ADDRESSES: %m"); + if (r <= 0) + break; - r = sscanf(prefixlen_str, "%hhu", &prefixlen); - if (r != 1) { - log_link_error(link, "Failed to parse prefixlen %s", prefixlen_str); - continue; - } + prefixlen_str = strchr(address_str, '/'); + if (!prefixlen_str) { + log_link_debug(link, "Failed to parse address and prefix length %s", address_str); + continue; + } + *prefixlen_str++ = '\0'; - r = in_addr_from_string_auto(address_str, &family, &address); - if (r < 0) { - log_link_debug_errno(link, r, "Failed to parse address %s: %m", address_str); - continue; - } + r = sscanf(prefixlen_str, "%hhu", &prefixlen); + if (r != 1) { + log_link_error(link, "Failed to parse prefixlen %s", prefixlen_str); + continue; + } - r = address_add(link, family, &address, prefixlen, NULL); - if (r < 0) - return log_link_error_errno(link, r, "Failed to add address: %m"); + r = in_addr_from_string_auto(address_str, &family, &address); + if (r < 0) { + log_link_debug_errno(link, r, "Failed to parse address %s: %m", address_str); + continue; } - } - if (routes) { - p = routes; + r = address_add(link, family, &address, prefixlen, NULL); + if (r < 0) + return log_link_error_errno(link, r, "Failed to add address: %m"); + } - for (;;) { - _cleanup_(sd_event_source_unrefp) sd_event_source *expire = NULL; - _cleanup_(route_freep) Route *tmp = NULL; - _cleanup_free_ char *route_str = NULL; - char *prefixlen_str; - Route *route; + for (const char *p = routes; p; ) { + _cleanup_(sd_event_source_unrefp) sd_event_source *expire = NULL; + _cleanup_(route_freep) Route *tmp = NULL; + _cleanup_free_ char *route_str = NULL; + char *prefixlen_str; + Route *route; - r = extract_first_word(&p, &route_str, NULL, 0); - if (r < 0) { - log_link_debug_errno(link, r, "Failed to extract next route string: %m"); - continue; - } - if (r == 0) - break; + r = extract_first_word(&p, &route_str, NULL, 0); + if (r < 0) + log_link_debug_errno(link, r, "failed to parse ROUTES: %m"); + if (r <= 0) + break; - prefixlen_str = strchr(route_str, '/'); - if (!prefixlen_str) { - log_link_debug(link, "Failed to parse route %s", route_str); - continue; - } + prefixlen_str = strchr(route_str, '/'); + if (!prefixlen_str) { + log_link_debug(link, "Failed to parse route %s", route_str); + continue; + } + *prefixlen_str++ = '\0'; - *prefixlen_str++ = '\0'; + r = route_new(&tmp); + if (r < 0) + return log_oom(); - r = route_new(&tmp); - if (r < 0) - return log_oom(); + r = sscanf(prefixlen_str, + "%hhu/%hhu/%"SCNu32"/%"PRIu32"/"USEC_FMT, + &tmp->dst_prefixlen, + &tmp->tos, + &tmp->priority, + &tmp->table, + &tmp->lifetime); + if (r != 5) { + log_link_debug(link, + "Failed to parse destination prefix length, tos, priority, table or expiration %s", + prefixlen_str); + continue; + } - r = sscanf(prefixlen_str, "%hhu/%hhu/%"SCNu32"/%"PRIu32"/"USEC_FMT, &tmp->dst_prefixlen, &tmp->tos, &tmp->priority, &tmp->table, &tmp->lifetime); - if (r != 5) { - log_link_debug(link, - "Failed to parse destination prefix length, tos, priority, table or expiration %s", - prefixlen_str); - continue; - } + r = in_addr_from_string_auto(route_str, &tmp->family, &tmp->dst); + if (r < 0) { + log_link_debug_errno(link, r, "Failed to parse route destination %s: %m", route_str); + continue; + } - r = in_addr_from_string_auto(route_str, &tmp->family, &tmp->dst); - if (r < 0) { - log_link_debug_errno(link, r, "Failed to parse route destination %s: %m", route_str); - continue; - } + r = route_add(link, tmp, &route); + if (r < 0) + return log_link_error_errno(link, r, "Failed to add route: %m"); - r = route_add(link, tmp, &route); + if (route->lifetime != USEC_INFINITY && !kernel_route_expiration_supported()) { + r = sd_event_add_time(link->manager->event, &expire, + clock_boottime_or_monotonic(), + route->lifetime, 0, route_expire_handler, route); if (r < 0) - return log_link_error_errno(link, r, "Failed to add route: %m"); - - if (route->lifetime != USEC_INFINITY && !kernel_route_expiration_supported()) { - r = sd_event_add_time(link->manager->event, &expire, clock_boottime_or_monotonic(), route->lifetime, - 0, route_expire_handler, route); - if (r < 0) - log_link_warning_errno(link, r, "Could not arm route expiration handler: %m"); - } - - sd_event_source_unref(route->expire); - route->expire = TAKE_PTR(expire); + log_link_warning_errno(link, r, "Could not arm route expiration handler: %m"); } + + sd_event_source_unref(route->expire); + route->expire = TAKE_PTR(expire); } if (dhcp4_address) {