Netdev List
 help / color / mirror / Atom feed
From: Simon Horman <horms@kernel.org>
To: Donald Hunter <donald.hunter@gmail.com>,
	 Jakub Kicinski <kuba@kernel.org>,
	"David S. Miller" <davem@davemloft.net>,
	 Eric Dumazet <edumazet@kernel.org>,
	Paolo Abeni <pabeni@redhat.com>
Cc: "Asbjørn Sloth Tønnesen" <ast@fiberby.net>,
	"Chuck Lever" <cel@kernel.org>,
	"Willem de Bruijn" <willemb@google.com>,
	netdev@vger.kernel.org, "Simon Horman" <horms@kernel.org>
Subject: [PATCH net-next v3] tools: ynl-gen: avoid shadow local variable declarations
Date: Tue, 01 Dec 2026 14:14:48 +0000	[thread overview]
Message-ID: <20261201-ynl-scope-v3-1-7a5588f05e8b@kernel.org> (raw)

Commit 099902fc66f8 ("tools: ynl-gen: avoid repetitive variables
definitions") moved away from block-scoped to function-scoped local
variables. However, it appears to have missed updating _multi_parse().
And with the following patch set applied, a shadow declaration of len
occurred for a string attribute.

- [PATCH net-next v3 00/10] Deliver TLS session tags to upper-layer consumers (NFSD)
  https://lore.kernel.org/all/20261113-tls-session-tags-v3-0-91271964cdb0@kernel.org/

Address this by using the same set-based mechanism used in the cited
commit to marshal local variables to be declared at the function-level.

The size_t variant of len is renamed as size to avoid the case where a
variable of the same name has two different types in the same scope.
This has not been observed in the wild AFAIK.

During review the topic of consistent code generation was raised.
Accordingly, unlike earlier revisions of this patch, minimal use of
var_set is made, leaving other variable handling as-is. And, for good
measure, var_set is sorted when appended to local_vars.

I am not marking this as a fix, as I do not believe the problem
described here manifests in-tree at this time.

Link: https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261123-ynl-scope-v2-1-f5b8c6bb31c1@kernel.org
Acked-by: Jakub Kicinski <kuba@kernel.org>
Signed-off-by: Simon Horman <horms@kernel.org>
---
Changes in v3:
- Preserve deterministic code creation (Sashiko)
- Also remove now unnecessary ri.cw.nl() from 'string' case (Sashiko)
- See: https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261123-ynl-scope-v2-1-f5b8c6bb31c1@kernel.org
- Link to v2: https://lore.kernel.org/r/20261123-ynl-scope-v2-1-f5b8c6bb31c1@kernel.org

Changes in v2:
- Use var.add(string) rather than var | set([string]) to add string
  elements to set.
  See: https://docs.astral.sh/ruff/rules/unnecessary-literal-set/
- Edited commit message
- Dropped Fixes tag
- Included Ack from Jakub
- Link to v1: https://lore.kernel.org/r/20261119-ynl-scope-v1-1-d3b0c964d4cb@kernel.org
---
 tools/net/ynl/pyynl/ynl_gen_c.py | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)

diff --git a/tools/net/ynl/pyynl/ynl_gen_c.py b/tools/net/ynl/pyynl/ynl_gen_c.py
index 4aca51396a9e..a609dfd24421 100755
--- a/tools/net/ynl/pyynl/ynl_gen_c.py
+++ b/tools/net/ynl/pyynl/ynl_gen_c.py
@@ -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)
@@ -2304,14 +2311,12 @@ def _multi_parse(ri, struct, init_lines, local_vars):
         elif aspec.type in scalars:
             ri.cw.p(f"dst->{aspec.c_name}[i] = ynl_attr_get_{aspec.type}(attr);")
         elif aspec.type == 'binary' and 'struct' in aspec:
-            ri.cw.p('size_t len = ynl_attr_data_len(attr);')
+            ri.cw.p('size = ynl_attr_data_len(attr);')
             ri.cw.nl()
-            ri.cw.p(f'if (len > sizeof(dst->{aspec.c_name}[0]))')
-            ri.cw.p(f'len = sizeof(dst->{aspec.c_name}[0]);')
-            ri.cw.p(f"memcpy(&dst->{aspec.c_name}[i], ynl_attr_data(attr), len);")
+            ri.cw.p(f'if (size > sizeof(dst->{aspec.c_name}[0]))')
+            ri.cw.p(f'size = sizeof(dst->{aspec.c_name}[0]);')
+            ri.cw.p(f"memcpy(&dst->{aspec.c_name}[i], ynl_attr_data(attr), size);")
         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));')
             ri.cw.p(f'dst->{aspec.c_name}[i] = malloc(sizeof(struct ynl_string) + len + 1);')
             ri.cw.p(f"if (!dst->{aspec.c_name}[i])")




                 reply	other threads:[~2026-12-01 14:15 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261201-ynl-scope-v3-1-7a5588f05e8b@kernel.org \
    --to=horms@kernel.org \
    --cc=ast@fiberby.net \
    --cc=cel@kernel.org \
    --cc=davem@davemloft.net \
    --cc=donald.hunter@gmail.com \
    --cc=edumazet@kernel.org \
    --cc=kuba@kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=willemb@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox