[PATCH 07/17] genksyms: restrict direct-abstract-declarator to take one parameter-type-list

From: Masahiro Yamada
Date: Mon Jan 13 2025 - 10:12:07 EST


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@xxxxxxxxxx>
---

scripts/genksyms/parse.y | 16 ++++++++++------
1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/scripts/genksyms/parse.y b/scripts/genksyms/parse.y
index fafce939c32f..03cdd8d53c13 100644
--- a/scripts/genksyms/parse.y
+++ b/scripts/genksyms/parse.y
@@ -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 ')'
--
2.43.0