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=GAIJey68; dkim-atps=neutral Received: from mail.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id C17FC5A0274 for ; Wed, 01 Jul 2026 07:32:09 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=202606; t=1782883921; bh=oGkAW7HPQlD3SDC5hG1qEEVQJZpICdLwO0JHpNSL73M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GAIJey683JntmIetGfy0E9aKiJoGIt8FH70oi8OjQ45fguNz9QsSxW6Du8UPlDElB ukxxk5LrogA5zGVou7AZIfYBb7vuHgVhGE4S+sGBnbnnT1buK/gz7+wt36tA7dLvM8 GuIGgmOiQqh9tNeGqTLbPi3i5QtfTGvyUvR07R0jgrjDJT5qKMCraTFadcWbFl6Ofu sBymi7HQp35cPAVTvLBXKvpAhCQQ4zHtJU3Az9mItbJ3npjHB3Hp0Uo8Kmy3fN1vEd HrLRkZaaVRnynqQWY5Gw1L7SZ3oPcm0kht1ZjUoyue3VAbv9GPaggYvPXmIKyPo30N eIAJQO0AE9X3w== Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4gqpYF0m3Wz58dY; Wed, 01 Jul 2026 15:32:01 +1000 (AEST) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH v2 05/13] conf: Clean up conf_ip4_prefix() Date: Wed, 1 Jul 2026 15:31:47 +1000 Message-ID: <20260701053155.1219264-6-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260701053155.1219264-1-david@gibson.dropbear.id.au> References: <20260701053155.1219264-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: EZOCYMPGKOK5RWYWHN6NJDDK4232R43R X-Message-ID-Hash: EZOCYMPGKOK5RWYWHN6NJDDK4232R43R 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.54.0