🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #25314

Re: [PATCH net-next v2] tools: ynl-gen: avoid shadow local variable declarations

#25314
From: Simon Horman
Date: Tue, 01 Dec 2026 14:03
154 lines
13815 bytes
On Mon, Nov 30, 2026 at 09:52:21PM -0500, willemb@google.com wrote:
> netdev-bot+sashiko@ wrote:
> > Thank you for your contribution! Sashiko AI review found 2 potential
> > issue(s) to consider.
> 
> > Critical: 0 · High: 0 · Medium: 1 · Low: 1
> 
> > If you believe none of the issues are legitimate, please reply to each
> > issue of Medium severity or higher and explain why the AI is wrong.
> > If any of them are legitimate and you plan to respin, please let patchwork
> > know by including "pw-bot: cr" as a separate line at the end of your reply
> > (one such reply per series is enough).
> 
> > - [Medium] In `_multi_parse()` (tools/net/ynl/pyynl/ynl_gen_c.py), the
> >    patch moves the per-attribute counter declarations `unsigned int…
> > - [Low] In `_multi_parse()`, the patch removes the block-local
> >    `ri.cw.p('unsigned int len;')` from the multi-attr string branch but…
> 
> > --
> 
> > tools: ynl-gen: avoid shadow local variable declarations
> 
> > This commit moves the block-scoped len declarations in _multi_parse() to
> > function scope, using the set-based mechanism from commit 099902fc66f8. It
> > also renames the size_t variant of len to size, so two variables with
> > different types don't share a name in the same scope.
> 
> > > diff --git a/tools/net/ynl/pyynl/ynl_gen_c.py
> > b/tools/net/ynl/pyynl/ynl_gen_c.py
> > > index 4aca51396a9e2..80b541619df49 100755
> > > --- a/tools/net/ynl/pyynl/ynl_gen_c.py
> > > +++ b/tools/net/ynl/pyynl/ynl_gen_c.py
> 
> > [ ... ]
> 
> > > @@ -2218,7 +2218,14 @@ def _multi_parse(ri, struct, init_lines,
> > local_vars):
> > >      all_multi = indexed_arrays | multi_attrs
> > >
> > >      for arg in sorted(all_multi):
> > > -        local_vars.append(f"unsigned int n_{struct[arg].c_name} = 0;")
> > > +        aspec = struct[arg]
> > > +        var_set.add(f'unsigned int n_{struct[arg].c_name} = 0;')
> > > +        if aspec.type == 'binary' and 'struct' in aspec:
> > > +            var_set.add('size_t size;')
> > > +        elif aspec.type == 'string':
> > > +            var_set.add('unsigned int len;')
> > > +
> > > +    local_vars += list(var_set)
> 
> > [Severity: Medium]
> 
> > Can this make the generator output differ from run to run?
> 
> > The n_ counter declarations used to be appended in sorted(all_multi)
> > order. Now they go into var_set. The order of list(var_set) follows
> > Python's set iteration order, which depends on the randomized str hash
> > (PYTHONHASHSEED).
> 
> > After that, CodeWriter.write_func_lvar() only does:
> 
> >          local_vars.sort(key=len, reverse=True)
> 
> > That sort is stable and uses only the length. Declarations of equal length
> > keep whatever order the set gave them.
> 
> > This looks reachable in-tree today. In
> > Documentation/netlink/specs/nfsd.yaml,
> > the server-stats-get dump reply has the multi-attr members proc2-ops,
> > proc3-ops and proc4-ops. They generate:
> 
> >      unsigned int n_proc2_ops = 0;
> >      unsigned int n_proc3_ops = 0;
> >      unsigned int n_proc4_ops = 0;
> 
> > All three have the same length. Their order in the generated nfsd-user.c
> > parse function can change between runs on the same spec. That hurts
> > reproducible builds of the YNL user library. It also defeats the
> > filecmp.cmp() check in CodeWriter that skips rewriting unchanged output.
> 
> > The n_ names are already unique per attribute, so they don't need
> > deduplication; only len and size do. Would it work to keep
> > local_vars.append() for the n_ counters, or to use
> > local_vars += sorted(var_set) instead?
> 
> > The commit message also doesn't mention that the n_ declarations now go
> > into the set.
> 
> PTAL at this sashiko comment

Thanks,

Sorry for entirely missing that this is a problem when writing the patch,
and that these comments are here on the ML.

I think a good solution is to leave the local_vars.append(f"unsigned int...")
logic as is. And for good measure to sort var_set, just in case there
are members of the same length in future.

This yields the following revised first-two hunks for the patch.

@@ -2208,7 +2208,7 @@ def _multi_parse(ri, struct, init_lines, local_vars):
             var_set |= set(l_vars) if l_vars else set()
         except Exception:
             pass  # _attr_get() not implemented by simple types, ignore
-    local_vars += list(var_set)
+
     if indexed_arrays or multi_attrs:
         local_vars.append('int i;')
     if needs_parg:
@@ -2219,6 +2219,13 @@ def _multi_parse(ri, struct, init_lines, local_vars):
 
     for arg in sorted(all_multi):
         local_vars.append(f"unsigned int n_{struct[arg].c_name} = 0;")
+        aspec = struct[arg]
+        if aspec.type == 'binary' and 'struct' in aspec:
+            var_set.add('size_t size;')
+        elif aspec.type == 'string':
+            var_set.add('unsigned int len;')
+
+    local_vars += sorted(var_set)
 
     ri.cw.block_start()
     ri.cw.write_func_lvar(local_vars)


> 
> > >
> > >      ri.cw.block_start()
> > >      ri.cw.write_func_lvar(local_vars)
> > > @@ -2304,13 +2311,12 @@ def _multi_parse(ri, struct, init_lines,
> > local_vars):
> 
> > [ ... ]
> 
> > >          elif aspec.type == 'string':
> > > -            ri.cw.p('unsigned int len;')
> > >              ri.cw.nl()
> > >              ri.cw.p('len = strnlen(ynl_attr_get_str(attr),
> > ynl_attr_data_len(attr));')
> 
> > [Severity: Low]
> 
> > This isn't a bug, but should the ri.cw.nl() be removed along with the
> > declaration?
> 
> > This branch is emitted right after block_start() opens the
> > if (ynl_attr_type(attr) == ...) block. CodeWriter.p() writes the pending
> > newline before the next line, so the generated C now has a blank line
> > right after the opening brace. checkpatch flags that ("Blank lines aren't
> > necessary after an open brace").

Yes, that is so. I'll drop the ri.qw.nl() in the next revision.
Technical Details
Message-ID: <20261201140355.GB804035@horms.kernel.org>
Path: mbox-mo1.pugleaf.net!news-wdc.pugleaf.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-375136-linux-kernel-netdev=dewy.ru
References: <20261123-ynl-scope-v2-1-f5b8c6bb31c1@kernel.org> <179553560643.3135.4325165389540156144@kernel.org> <willemb.1bc16eae1189b@google.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-wdc.pugleaf.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-375136-linux-kernel-netdev=dewy.ru
From: horms@kernel.org (Simon Horman)
Newsgroups: linux.kernel.netdev
Subject: Re: [PATCH net-next v2] tools: ynl-gen: avoid shadow local variable
 declarations
Date: Tue, 1 Dec 2026 14:03:55 +0000
Organization: Usenet Network
Message-ID: <20261201140355.GB804035@horms.kernel.org>
References: <20261123-ynl-scope-v2-1-f5b8c6bb31c1@kernel.org>
 <179553560643.3135.4325165389540156144@kernel.org>
 <willemb.1bc16eae1189b@google.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="1729370"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:npltx7LHCs/B8R48BRwTZTspKoo= sha256:31bmQeeYeE9FLIyu8zrFE9mnht9SzpZgDHLsNlWm7TE=
	sha1:22XcfpUV3JtRzCHp4juAypZzraA= sha256:5lamJHhz2l/cTMRjiC2RoZC3k8RNOEDck8ZEuRDSTm8=
Content-Disposition: inline
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id 91C221173CD; Tue,  1 Dec 2026 17:04:20 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.401
X-Spam-Status: No, score=-3.401 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1,
	DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,
	MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
	SPF_PASS=-0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=kernel.org
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id rqo5v2F33owy;
	Tue,  1 Dec 2026 17:04:17 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-375136-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
	by mail.dewy.ru (Postfix) with ESMTPS id B31BB1173CE; Tue,  1 Dec 2026 17:04:03 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sea.lore.kernel.org (Postfix) with ESMTP id 8B7DF2611D; Tue,  1 Dec 2026 14:04:01 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1796133841;
	b=Ettd6+iXnIiq1Nd63rOXGQ2bqsBjcr7+4cEwXaeLW1Bd7iBxIsCNZFHx5Nh0mow9IVFd
	 qI0q0B0CyUVsYQ/vOzIsADKABvL258LKea3+aKvMOECWZfI0b89fljTJRWuc05e6muPow
	 AW4ZkOkAAZWadFBm/gq82eEjNOFtbL8ghvwrdk4YcS2wjE8al0k6fTcrpLcRad5uxxaeb
	 KJdeNYHdJsJ/iAdFZSVQuw9Ok2ko9oo3IIDgbtbVqjQqA4qpYEQHgmjjxDxQXmYnIZ3up
	 vWCWaPVP1Jc9TkeWRAlHlrYRl8YcHZ3pFq9m2+6DRurjH5MVNtSnEeHmHuVoUwVXqMA==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796133841;
	h=X-Original-To:Received:Received:DKIM-Signature:Date:From:To:Cc:
	 Subject:Message-ID:References:X-Mailing-List:List-Id:List-Post:
	 List-Help:List-Subscribe:List-Unsubscribe:List-Owner:Precedence:
	 MIME-Version:Content-Type:Content-Disposition:
	 Content-Transfer-Encoding:In-Reply-To;
	bh=jg5KBRZ2NvBMABaCNL7qKv3HyvEufpXlVZUQgYchd5s=;
	b=Saojfl5OcNvi9ffwJ6D74NjLe5EEihoeqdvVngDRwA8/g3B5O9HbgfjCoQSym2CQNLBh
	 eJHhuidWyIxRP1Ewzjt2jGc8S+Pz6v523ajx7HS42JPeqO6RpcvVYa3foIwBv3pMmuOgw
	 DgGm6TJm8stu8kPUGmS1QuFbPZCSBlg3NJzQo7WV54mdf54BWlx1l1LC4qdXApEiulhNp
	 5KGv8o1CG3HAJ+ZMhIPjtv0SFaftmLjEXk6+TM7w4QVwzj67Ls4Hf2JmnzwYd24mcQm9V
	 aDlNZceJLIbMx6WOgTjaWvkRLQjntaUoceq2RTVmYwWdxX1GG70QSj0Z0ZKjVsR+4fg==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=MbbPcetp;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id 60C2B606A32A;
	Tue,  1 Dec 2026 14:04:01 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=MbbPcetp
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1796133840;
	b=A1T1+zFTcRqPWeotKwkuX4hQj4uxC6ZwCAVJd3TkuoE1A7bAqLHNsgIll9Am6vC2B0Pe
	 1ixfxaGa+ZSTVjIjNZJIv3JfP8rpG5IToXRonjvw+FjhDTnGUSeIw6mV1LRljuobcT9mT
	 JoJG+nbqhHASJuqMmWbLWmABcE0c8vsBNp+96nPgLM9X0ATxy9IDfO3rJMmHH0TbtSCmh
	 BqbkwI8Wa/H6s7qDJ6cnT8uvANOrH48C1vdJgFZJdG/VqY7X5jpXHtxgUZfgzQ9qXYUkt
	 DY6klrL8Hdh/naBZY/hbz2YFh66iC7hLq7YwQ2efzyv1A08+xZs42BkDEWsAStwC81Q==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796133840;
	h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID:
	 References:MIME-Version:Content-Type:Content-Disposition:
	 Content-Transfer-Encoding:In-Reply-To;
	bh=jg5KBRZ2NvBMABaCNL7qKv3HyvEufpXlVZUQgYchd5s=;
	b=pfz9usRhw90hJ1b8EyltEmDIBA+fz2DcA+lSemJaf4jwe31rabFD1YV3ctgQ8QpCV4M7
	 zCzI3DD4Zgwu0kx0VmounRrd04LAB+MyQleaWVkr6BmTJhwoHh5Wdp+YS+yRoVLB+6PoT
	 yLynICuHKMI8/l5oW73MUpAiygKcPCnR6Ls+qmIQqvuJ2iZa/yeeg/t3+qfvVN6lT0Pal
	 2T5vlEYrcz24ck64C5TqTWHNKaMz0Lxjo6esHjC43i0KqT5wrPRvDeEgEOghGc8o/dMBH
	 x1zCdSDMgXHOjcyPmIYct1TxbtuOUFOzYDHwjZrZr7yW1J+M0wQfwNsdtA6lyM2RGWQ==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=MbbPcetp;
	arc=none smtp.remote-ip=100.103.45.18
X-Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1))
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7DA21606A31A
	for <netdev@vger.kernel.org>; Tue,  1 Dec 2026 14:04:00 +0000 (UTC)
X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 66C2A1F00893;
	Tue,  1 Dec 2026 14:03:58 +0000 (UTC)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org;
	s=k20260515; t=1796133840;
	bh=jg5KBRZ2NvBMABaCNL7qKv3HyvEufpXlVZUQgYchd5s=;
	h=Date:From:To:Cc:Subject:References:In-Reply-To;
	b=MbbPcetpKQphE3XPtsw0EDYYlQ/0u8jFcm+6k/C4c+z3jAJ+vbXu5XpYOGSd4sBr3
	 R8DTlHVzbngaV6gpzbbvhd5I1Hsb0BirGj0u2MAQBrQbb39Uqh7X+TdeVk87kFTx49
	 VRxE2VeJq3cawG4q/xiS+QyFtKkJ+7onwM8Rnm2SwSG9077dXcWJPJiQOpJiM0WaWB
	 X3kbPyDoESJIbcAj0ZEpWZzolntdXSlJFZc85O4OVaFouGLU0rCUfHYrWax12sDHZA
	 C5UKzRsxEMtU0bMbJVxDEdbU8+KeyFccv1Xfa0CJd/tPW1UYjlyG5u90YD5icrkQaU
	 PfaUi9chq6HDw==
X-Cc: netdev-bot+sashiko@kernel.org, donald.hunter@gmail.com, kuba@kernel.org,
	davem@davemloft.net, edumazet@kernel.org, pabeni@redhat.com,
	ast@fiberby.net, cel@kernel.org, netdev@vger.kernel.org
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list
X-In-Reply-To: <willemb.1bc16eae1189b@google.com>