genksyms: restrict direct-abstract-declarator to take one parameter-type-list

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>
This commit is contained in:
Masahiro Yamada 2025-01-14 00:00:45 +09:00
parent a95298656c
commit c2f1846ba8
1 changed files with 10 additions and 6 deletions

View 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 ')'
| direct_abstract_declarator1 open_paren error ')'
{ $$ = $4; }
| direct_abstract_declarator 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 ')'