Skip to content

Commit

Permalink
parser-type: make squares detection more stringent
Browse files Browse the repository at this point in the history
  • Loading branch information
mptre committed Aug 24, 2024
1 parent 63950b6 commit c66bba7
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
22 changes: 20 additions & 2 deletions parser-type.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ static int peek_type_ptr_array(struct lexer *, struct token **);
static int peek_type_noident(struct lexer *, struct token **);
static int peek_type_unknown_array(struct lexer *, struct token **);
static int peek_type_unknown_bitfield(struct lexer *, struct token **);
static int peek_type_squares(struct lexer *, struct token **);

int
parser_type_peek(struct parser *pr, struct parser_type *type,
Expand Down Expand Up @@ -74,8 +75,9 @@ parser_type_peek(struct parser *pr, struct parser_type *type,
end->tk_type == TOKEN_UNION)
(void)lexer_if(lx, TOKEN_IDENT, &end);
/* Recognize constructs like `struct s[]'. */
(void)lexer_if_pair(lx, TOKEN_LSQUARE, TOKEN_RSQUARE,
NULL, &end);
if (peek_type_squares(lx, &rsquare) &&
lexer_seek_after(lx, rsquare))
end = rsquare;
peek = 1;
} else if (ntokens > 0 && lexer_if(lx, TOKEN_STAR, &end)) {
/*
Expand Down Expand Up @@ -438,3 +440,19 @@ peek_type_unknown_bitfield(struct lexer *lx, struct token **tk)
lexer_peek_leave(lx, &s);
return peek;
}

static int
peek_type_squares(struct lexer *lx, struct token **rsquare)
{
struct lexer_state ls;
int peek = 0;

lexer_peek_enter(lx, &ls);
if (lexer_if(lx, TOKEN_LSQUARE, NULL)) {
(void)lexer_if(lx, TOKEN_LITERAL, NULL);
if (lexer_if(lx, TOKEN_RSQUARE, rsquare))
peek = 1;
}
lexer_peek_leave(lx, &ls);
return peek;
}
1 change: 1 addition & 0 deletions t.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ main(void)
"int");
test_parser_type("u_int:4", "u_int");
test_parser_type("LIST_ENTRY(list, s);", "LIST_ENTRY ( list , s )");
test_parser_type("union [)]", "union");

test_parser_type_flags(PARSER_TYPE_CAST,
"const foo_t)", "const foo_t");
Expand Down
1 change: 1 addition & 0 deletions tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ TESTS+= error-060.c
TESTS+= error-061.c
TESTS+= error-062.c
TESTS+= error-063.c
TESTS+= error-064.c
TESTS+= error-065.c
TESTS+= error-066.c
TESTS+= error-067.c
Expand Down
5 changes: 5 additions & 0 deletions tests/error-064.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/*
* AFL
*/

n(/**/union[);]

0 comments on commit c66bba7

Please sign in to comment.