genksyms: restrict direct-abstract-declarator to take one parameter-type-list
authorMasahiro Yamada <masahiroy@kernel.org>
Mon, 13 Jan 2025 15:00:45 +0000 (00:00 +0900)
committerMasahiro Yamada <masahiroy@kernel.org>
Sat, 18 Jan 2025 00:11:46 +0000 (09:11 +0900)
While there is no more grammatical ambiguity in genksyms, the parser
logic is still inaccurate.

For example, genksyms accepts the following invalid C code:

    void my_func(int ()(int));

This should result in a syntax error because () cannot be reduced to
<direct-abstract-declarator>.

( <abstract-declarator> ) can be reduced, but <abstract-declarator>
must not be empty in the following grammar from K&R [1]:

  <direct-abstract-declarator> ::=  ( <abstract-declarator> )
                                 | {<direct-abstract-declarator>}? [ {<constant-expression>}? ]
                                 | {<direct-abstract-declarator>}? ( {<parameter-type-list>}? )

Furthermore, genksyms accepts the following weird code:

    void my_func(int (*callback)(int)(int)(int));

The parser allows <direct-abstract-declarator> to recursively absorb
multiple ( {<parameter-type-list>}? ), but this behavior is incorrect.

In the example above, (*callback) should be followed by at most one
(int).

[1]: https://cs.wmich.edu/~gupta/teaching/cs4850/sumII06/The%20syntax%20of%20C%20in%20Backus-Naur%20form.htm

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
Acked-by: Nicolas Schier <n.schier@avm.de>
scripts/genksyms/parse.y

index fafce939c32f7130af7eff60a36026e6558eb20c..03cdd8d53c13af5d9ab82109705ec60f61fa92f0 100644 (file)
@@ -381,20 +381,24 @@ abstract_declarator:
        ;
 
 direct_abstract_declarator:
+       direct_abstract_declarator1
+       | direct_abstract_declarator1 open_paren parameter_declaration_clause ')'
+               { $$ = $4; }
+       | open_paren parameter_declaration_clause ')'
+               { $$ = $3; }
+       ;
+
+direct_abstract_declarator1:
          IDENT
                { /* For version 2 checksums, we don't want to remember
                     private parameter names.  */
                  remove_node($1);
                  $$ = $1;
                }
-       | direct_abstract_declarator open_paren parameter_declaration_clause ')'
-               { $$ = $4; }
-       | direct_abstract_declarator open_paren error ')'
+       | direct_abstract_declarator1 open_paren error ')'
                { $$ = $4; }
-       | direct_abstract_declarator BRACKET_PHRASE
+       | direct_abstract_declarator1 BRACKET_PHRASE
                { $$ = $2; }
-       | open_paren parameter_declaration_clause ')'
-               { $$ = $3; }
        | open_paren abstract_declarator ')'
                { $$ = $3; }
        | open_paren error ')'