Linux SPARSE checker discussions
 help / color / mirror / Atom feed
* [PATCH] sparse/dissect: fix parsing of array designated initializers
@ 2026-05-10 15:06 Oleg Nesterov
  0 siblings, 0 replies; only message in thread
From: Oleg Nesterov @ 2026-05-10 15:06 UTC (permalink / raw)
  To: Chris Li, Luc Van Oostenryck; +Cc: Alexey Gladkov, linux-sparse

dissect.c fails to parse nested designated initializers that contain array
indices. In this case do_initializer() is called recursively with type ==
EXPR_INDEX, this later calls do_expression(EXPR_IDENTIFIER).

Change the "while (m_expr->type == EXPR_IDENTIFIER)" loop to unwrap the
EXPR_INDEX expressions as well.

Minimal test-case:

	struct O {
		struct I { int mem; } ary[1];
	} v = {
		.ary[0].mem = 0,
	};

Before the patch:

    $ ./test-dissect TEST.c
    1:8                    def  s O
    2:16                   def  s I
    2:24                   def  m I.mem                              int
    2:31                   def  m O.ary                              struct I [1]
    3:3                    def  v v                                  struct O
    3:3                    -w-  v v                                  struct O
    4:10  v                -w-  m O.ary                              struct I [1]
    TEST.c:4:17: warning: bad expr->type: 25

the warning comes from do_expression(), 25 is EXPR_IDENTIFIER.
The usage of I.mem is not reported.

After the patch:

    $ ./test-dissect TEST.c
    1:8                    def  s O
    2:16                   def  s I
    2:24                   def  m I.mem                              int
    2:31                   def  m O.ary                              struct I [1]
    3:3                    def  v v                                  struct O
    3:3                    -w-  v v                                  struct O
    4:10  v                -w-  m O.ary                              struct I [1]
    4:17  v                -w-  m I.mem                              int

Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
 dissect.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/dissect.c b/dissect.c
index da1b63c8..796b6e4d 100644
--- a/dissect.c
+++ b/dissect.c
@@ -602,6 +602,11 @@ static struct symbol *do_initializer(struct symbol *type, struct expression *exp
 							lookup_member(m_type, m_expr->expr_ident, m_atop));
 					m_expr = m_expr->ident_expression;
 					m_atop = NULL;
+
+					while (m_expr->type == EXPR_INDEX) {
+						m_type = base_type(m_type);
+						m_expr = m_expr->idx_expression;
+					}
 				}
 
 				if (m_atop) {
-- 
2.52.0



^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-05-10 15:06 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-10 15:06 [PATCH] sparse/dissect: fix parsing of array designated initializers Oleg Nesterov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox