From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=none (p=none dis=none) header.from=gibson.dropbear.id.au Authentication-Results: passt.top; dkim=pass (2048-bit key; secure) header.d=gibson.dropbear.id.au header.i=@gibson.dropbear.id.au header.a=rsa-sha256 header.s=202606 header.b=SSssIII2; dkim-atps=neutral Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 0FDBB5A0275 for ; Fri, 03 Jul 2026 05:55:00 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202606; t=1783050892; bh=2kSXNJvGkk02kJRTBSe9Ugpc3GEoZwpOGk3MbnTuUkI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SSssIII26J7t6vcflUfDL/kH/+MVoMvEu6LAoqh2Ia95n69WogBpenYpwqD7b6Gfu W67gZ0LKgZjon/+2tLumPpnQfZzSySp7Dk+gkBhPkSYuX0LJ/mng6DrCis1rTPOVcr +NoG+8y/mQXc6rFFHNKCod+Ogz/Nq4TtBvU856ounmmhrulO6Z0ZYELTtGWU653t7P AWX7AZ8wiMCgk5DXWmVuKa3OjxOesICwTfb5DP+eYiRNbIjmiyAgJWUvCz0jipfnHF 8zmlAHavy1CW73ZDNmrODDYfFs+1Fx4fD/uIlALRPXZwgoLyIQ1C5dZgglVNMgJvRZ X+vLw4yzbIncw== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4gs0JD23qwz58nG; Fri, 03 Jul 2026 13:54:52 +1000 (AEST) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH v5 05/13] conf: Clean up conf_ip4_prefix() Date: Fri, 3 Jul 2026 13:54:37 +1000 Message-ID: <20260703035445.888394-6-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260703035445.888394-1-david@gibson.dropbear.id.au> References: <20260703035445.888394-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: U2JWR5J755TIQUDZTCKCCGLLQQ4ZVQXY X-Message-ID-Hash: U2JWR5J755TIQUDZTCKCCGLLQQ4ZVQXY X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: David Gibson X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Restructure conf_ip4_prefix() so that success is the early exit path and failure is the "default" path. Also move the error handling (die()) into it from the caller. This saves a handful of lines for now, and will make integration with upcoming parsing changes nicer. Signed-off-by: David Gibson --- conf.c | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/conf.c b/conf.c index 32163f56..ea299e18 100644 --- a/conf.c +++ b/conf.c @@ -350,9 +350,9 @@ static void conf_pasta_ns(int *netns_only, char *userns, char *netns, /** conf_ip4_prefix() - Parse an IPv4 prefix length or netmask * @arg: Netmask in dotted decimal or prefix length * - * Return: validated prefix length on success, -1 on failure + * Return: validated prefix length; dies on bad argument */ -static int conf_ip4_prefix(const char *arg) +static uint8_t conf_ip4_prefix(const char *arg) { struct in_addr mask; unsigned long len; @@ -360,16 +360,16 @@ static int conf_ip4_prefix(const char *arg) if (inet_pton(AF_INET, arg, &mask)) { in_addr_t hmask = ntohl(mask.s_addr); len = __builtin_popcount(hmask); - if ((hmask << len) != 0) - return -1; + if ((hmask << len) == 0) + return len; } else { errno = 0; len = strtoul(arg, NULL, 0); - if (len > 32 || errno) - return -1; + if (!errno && len <= 32) + return len; } - return len; + die("Invalid prefix length: %s", arg); } /** @@ -1606,20 +1606,13 @@ void conf(struct ctx *c, int argc, char **argv) } break; } - case 'n': { - int plen; - + case 'n': if (addr_has_prefix_len) die("Redundant prefix length specification"); - plen = conf_ip4_prefix(optarg); - if (plen < 0) - die("Invalid prefix length: %s", optarg); - - prefix_len_from_opt = plen + 96; - c->ip4.prefix_len = plen; + c->ip4.prefix_len = conf_ip4_prefix(optarg); + prefix_len_from_opt = c->ip4.prefix_len + 96; break; - } case 'M': parse_mac(c->our_tap_mac, optarg); break; -- 2.55.0