Thread View: linux.kernel.netdev
3 messages
3 total messages
Started by Simon Horman
Mon, 23 Nov 2026 15:52
[PATCH net-next v2] tools: ynl-gen: avoid shadow local variable declarations
Author: Simon Horman
Date: Mon, 23 Nov 2026 15:52
Date: Mon, 23 Nov 2026 15:52
82 lines
11627 bytes
11627 bytes
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.
I am not marking this as a fix, as I do not believe the problem
described here manifests in-tree at this time.
Acked-by: Jakub Kicinski <kuba@kernel.org>
Signed-off-by: Simon Horman <horms@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 | 20 +++++++++++++-------
1 file changed, 13 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..80b541619df4 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:
@@ -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)
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 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);')
Re: [PATCH net-next v2] tools: ynl-gen: avoid shadow local variable declarations
Author: netdev-bot+sashiko
Date: Tue, 24 Nov 2026 15:53
Date: Tue, 24 Nov 2026 15:53
109 lines
11976 bytes
11976 bytes
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.
>
> 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").
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261123-ynl-scope-v2-1-f5b8c6bb31c1%40kernel.org
Re: [PATCH net-next v2] tools: ynl-gen: avoid shadow local variable declarations
Author: Simon Horman
Date: Tue, 01 Dec 2026 14:03
Date: Tue, 01 Dec 2026 14:03
154 lines
13815 bytes
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.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads