tools: ynl-gen: avoid shadow local variable declarations
Subsystems (3)
Simon Horman
reviewerPATCH 1/1 tools: ynl-gen: avoid shadow local variable declarations
The cited commit 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/[email protected]/
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 need for a variable of the same name but different types in the same scope. This, however, has not been observed in the wild AFAIK.
I am marking this as a fix, as it does seem to be a bug in the cited commit. But targeting net-next as I do not believe it manifests in-tree at this time.
--- tools/net/ynl/pyynl/ynl_gen_c.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-)
tools/net/ynl/pyynl/ynl_gen_c.py +13 -7
@@ -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 |= set([f'unsigned int n_{struct[arg].c_name} = 0;']) + if aspec.type == 'binary' and 'struct' in aspec: + var_set |= set(['size_t size;']) + elif aspec.type == 'string': + var_set |= set(['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);')
tools/net/ynl/pyynl/ynl_gen_c.py def _multi_parse(ri, struct, init_lines, local_vars): · line 2226
Quoted 8 lines · On Thu, 19 Nov 2026 16:30:34 +0000 Simon Horman wrote:
for arg in
sorted(all_multi):tools/net/ynl/pyynl/ynl_gen_c.py +6
-1- local_vars.append(f"unsigned int n_{struct[arg].c_name} = 0;") + aspec = struct[arg] + var_set |= set([f'unsigned int n_{struct[arg].c_name} = 0;']) + if aspec.type == 'binary' and 'struct' in aspec: + var_set |= set(['size_t size;']) + elif aspec.type == 'string': + var_set |= set(['unsigned int len;'])
tools/net/ynl/pyynl/ynl_gen_c.py:2222: [C405] Unnecessary list literal (rewrite as a set literal) tools/net/ynl/pyynl/ynl_gen_c.py:2224: [C405] Unnecessary list literal (rewrite as a set literal) tools/net/ynl/pyynl/ynl_gen_c.py:2226: [C405] Unnecessary list literal (rewrite as a set literal)
Please drop the Fixes tag when reposting, and you can add
Signature · 1 line
pw-bot: cr