diff options
author | Bruno Haible <bruno@clisp.org> | 2023-03-08 21:53:49 +0100 |
---|---|---|
committer | Bruno Haible <bruno@clisp.org> | 2023-03-09 11:40:25 +0100 |
commit | e783e6a343cf8c7d3cf31873a73d4ba94712ba18 (patch) | |
tree | 9a9b7dbced760cae27da374ddf65529b8091eb88 | |
parent | db16c08cfe9f12150c8ca107796841f0185a3e44 (diff) | |
download | gettext-e783e6a343cf8c7d3cf31873a73d4ba94712ba18.tar.gz |
xgettext: In language Vala, avoid stack overflow.
* gettext-tools/src/x-vala.c (MAX_NESTING_DEPTH): New macro.
(nesting_depth): New variable.
(extract_balanced): Increase and check nesting_depth before calling
extract_balanced recursively.
(extract_vala): Initialize nesting_depth.
* gettext-tools/tests/xgettext-vala-stackovfl-1: New file.
* gettext-tools/tests/xgettext-vala-stackovfl-2: New file.
* gettext-tools/tests/Makefile.am (TESTS): Add them.
-rw-r--r-- | gettext-tools/src/x-vala.c | 17 | ||||
-rw-r--r-- | gettext-tools/tests/Makefile.am | 1 | ||||
-rwxr-xr-x | gettext-tools/tests/xgettext-vala-stackovfl-1 | 63 | ||||
-rwxr-xr-x | gettext-tools/tests/xgettext-vala-stackovfl-2 | 56 |
4 files changed, 136 insertions, 1 deletions
diff --git a/gettext-tools/src/x-vala.c b/gettext-tools/src/x-vala.c index e2754f73d..c26fc9673 100644 --- a/gettext-tools/src/x-vala.c +++ b/gettext-tools/src/x-vala.c @@ -1,5 +1,5 @@ /* xgettext Vala backend. - Copyright (C) 2013-2014, 2018-2020 Free Software Foundation, Inc. + Copyright (C) 2013-2014, 2018-2023 Free Software Foundation, Inc. This file was written by Daiki Ueno <ueno@gnu.org>, 2013. @@ -1203,6 +1203,13 @@ x_vala_lex (token_ty *tp) static flag_context_list_table_ty *flag_context_list_table; +/* Maximum supported nesting depth. */ +#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 @@ -1277,6 +1284,12 @@ extract_balanced (message_list_ty *mlp, token_type_ty delim, continue; case 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_balanced (mlp, token_type_rparen, inner_context, next_context_iter, arglist_parser_alloc (mlp, @@ -1285,6 +1298,7 @@ extract_balanced (message_list_ty *mlp, token_type_ty delim, arglist_parser_done (argparser, arg); return true; } + nesting_depth--; next_context_iter = null_context_list_iterator; state = 0; break; @@ -1402,6 +1416,7 @@ extract_vala (FILE *f, last_token_type = token_type_other; flag_context_list_table = flag_table; + nesting_depth = 0; init_keywords (); diff --git a/gettext-tools/tests/Makefile.am b/gettext-tools/tests/Makefile.am index 89751fbc8..1cad63813 100644 --- a/gettext-tools/tests/Makefile.am +++ b/gettext-tools/tests/Makefile.am @@ -149,6 +149,7 @@ TESTS = gettext-1 gettext-2 \ xgettext-stringtable-1 \ xgettext-tcl-1 xgettext-tcl-2 xgettext-tcl-3 xgettext-tcl-4 \ xgettext-vala-1 xgettext-vala-2 \ + xgettext-vala-stackovfl-1 xgettext-vala-stackovfl-2 \ xgettext-ycp-1 xgettext-ycp-2 xgettext-ycp-3 xgettext-ycp-4 \ format-awk-1 format-awk-2 \ format-boost-1 format-boost-2 \ diff --git a/gettext-tools/tests/xgettext-vala-stackovfl-1 b/gettext-tools/tests/xgettext-vala-stackovfl-1 new file mode 100755 index 000000000..9894418b1 --- /dev/null +++ b/gettext-tools/tests/xgettext-vala-stackovfl-1 @@ -0,0 +1,63 @@ +#! /bin/sh +. "${srcdir=.}/init.sh"; path_prepend_ . ../src + +# Test Vala support: stack overflow prevented by nesting depth check. + +cat <<EOF > xg-vala-so-1.vala +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +((((((((((((((((((((((((((((((((((((((((((((((((( +_("Hello!") +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +))))))))))))))))))))))))))))))))))))))))))))))))) +EOF + +: ${XGETTEXT=xgettext} +${XGETTEXT} --omit-header --no-location -d xg-vala-so-1.tmp xg-vala-so-1.vala || Exit 1 +LC_ALL=C tr -d '\r' < xg-vala-so-1.tmp.po > xg-vala-so-1.po || Exit 1 + +cat <<EOF > xg-vala-so-1.ok +msgid "Hello!" +msgstr "" +EOF + +: ${DIFF=diff} +${DIFF} xg-vala-so-1.ok xg-vala-so-1.po +result=$? + +exit $result diff --git a/gettext-tools/tests/xgettext-vala-stackovfl-2 b/gettext-tools/tests/xgettext-vala-stackovfl-2 new file mode 100755 index 000000000..44121b995 --- /dev/null +++ b/gettext-tools/tests/xgettext-vala-stackovfl-2 @@ -0,0 +1,56 @@ +#! /bin/sh +. "${srcdir=.}/init.sh"; path_prepend_ . ../src + +# Test Vala support: stack overflow prevented by nesting depth check. + +cat <<EOF > xg-vala-so-2.vala +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +_("Hello!") +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +EOF + +: ${XGETTEXT=xgettext} +${XGETTEXT} --omit-header --no-location -d xg-vala-so-2.tmp xg-vala-so-2.vala 2>xg-vala-so-2.err +result=$? +cat xg-vala-so-2.err +test $result = 1 || Exit 1 + +exit 0 |