diff options
| author | Bruno Haible <bruno@clisp.org> | 2023-03-06 18:54:00 +0100 |
|---|---|---|
| committer | Bruno Haible <bruno@clisp.org> | 2023-03-06 18:54:00 +0100 |
| commit | a6636c9a310a50cef71710008a7ff4bd62fe2b31 (patch) | |
| tree | 9a84b79a3dcc389626886e1868910ce56b30a58e /gettext-tools/src | |
| parent | 5cb3d9496eaae25e9298a76c21e59f8d122474d5 (diff) | |
| download | gettext-a6636c9a310a50cef71710008a7ff4bd62fe2b31.tar.gz | |
xgettext: In language C, avoid stack overflow.
* gettext-tools/src/x-c.c (MAX_NESTING_DEPTH): New macro.
(nesting_depth): New variable.
(extract_parenthesized): Increase and check nesting_depth before calling
extract_parenthesized recursively.
(extract_whole_file): Initialize nesting_depth.
* gettext-tools/tests/xgettext-c-stackovfl-1: New file.
* gettext-tools/tests/xgettext-c-stackovfl-2: New file.
* gettext-tools/tests/Makefile.am (TESTS): Add them.
Diffstat (limited to 'gettext-tools/src')
| -rw-r--r-- | gettext-tools/src/x-c.c | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/gettext-tools/src/x-c.c b/gettext-tools/src/x-c.c index 917bd7c88..b49ddd9cc 100644 --- a/gettext-tools/src/x-c.c +++ b/gettext-tools/src/x-c.c @@ -1,5 +1,5 @@ /* xgettext C/C++/ObjectiveC backend. - Copyright (C) 1995-1998, 2000-2009, 2012-2015, 2018-2020 Free Software Foundation, Inc. + Copyright (C) 1995-1998, 2000-2009, 2012-2015, 2018-2023 Free Software Foundation, Inc. This file was written by Peter Miller <millerp@canb.auug.org.au> @@ -2164,6 +2164,15 @@ x_c_lex (xgettext_token_ty *tp) static flag_context_list_table_ty *flag_context_list_table; +/* Maximum supported nesting depth. + ISO C 23 ยง 5.2.4.1.(1) requires 63 "nesting levels of parenthesized + expressions within a full expression"; then 1000 is more than enough. */ +#define MAX_NESTING_DEPTH 1000 + +/* Current nesting depth. */ +static int nesting_depth; + + /* The file is broken into tokens. Scan the token stream, looking for a keyword, followed by a left paren, followed by a string. When we see this sequence, we have something to remember. We assume we are @@ -2245,6 +2254,12 @@ extract_parenthesized (message_list_ty *mlp, continue; case xgettext_token_type_lparen: + if (++nesting_depth > MAX_NESTING_DEPTH) + { + error_with_progname = false; + error (EXIT_FAILURE, 0, _("%s:%d: error: too many open parentheses"), + logical_file_name, line_number); + } if (extract_parenthesized (mlp, inner_context, next_context_iter, arglist_parser_alloc (mlp, state ? next_shapes : NULL))) @@ -2252,6 +2267,7 @@ extract_parenthesized (message_list_ty *mlp, arglist_parser_done (argparser, arg); return true; } + nesting_depth--; next_context_iter = null_context_list_iterator; selectorcall_context_iter = null_context_list_iterator; state = 0; @@ -2356,6 +2372,7 @@ extract_whole_file (FILE *f, phase6_pushback_length = 0; flag_context_list_table = flag_table; + nesting_depth = 0; init_keywords (); |
