From: Bruno Haible Date: Wed, 8 Mar 2023 19:40:35 +0000 (+0100) Subject: xgettext: In language awk, avoid stack overflow. X-Git-Tag: v0.22~131 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cd298b41029697bd50906b1c3dded2501fb0f8b6;p=thirdparty%2Fgettext.git xgettext: In language awk, avoid stack overflow. * gettext-tools/src/x-awk.c (MAX_NESTING_DEPTH): New macro. (nesting_depth): New variable. (extract_parenthesized): Increase and check nesting_depth before calling extract_parenthesized recursively. (extract_awk): Initialize nesting_depth. * gettext-tools/tests/xgettext-awk-stackovfl-1: New file. * gettext-tools/tests/xgettext-awk-stackovfl-2: New file. * gettext-tools/tests/Makefile.am (TESTS): Add them. --- diff --git a/gettext-tools/src/x-awk.c b/gettext-tools/src/x-awk.c index c2b8ddcd8..1a2098e98 100644 --- a/gettext-tools/src/x-awk.c +++ b/gettext-tools/src/x-awk.c @@ -1,5 +1,5 @@ /* xgettext awk backend. - Copyright (C) 2002-2003, 2005-2009, 2018-2020 Free Software Foundation, Inc. + Copyright (C) 2002-2003, 2005-2009, 2018-2023 Free Software Foundation, Inc. This file was written by Bruno Haible , 2002. @@ -667,6 +667,13 @@ x_awk_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 @@ -756,6 +763,12 @@ extract_parenthesized (message_list_ty *mlp, 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_parenthesized (mlp, inner_context, next_context_iter, arglist_parser_alloc (mlp, state ? next_shapes : NULL))) @@ -763,6 +776,7 @@ extract_parenthesized (message_list_ty *mlp, arglist_parser_done (argparser, arg); return true; } + nesting_depth--; next_is_argument = false; next_context_iter = null_context_list_iterator; state = 0; @@ -879,6 +893,7 @@ extract_awk (FILE *f, prefer_division_over_regexp = false; 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 914143111..bd5e357c9 100644 --- a/gettext-tools/tests/Makefile.am +++ b/gettext-tools/tests/Makefile.am @@ -83,6 +83,7 @@ TESTS = gettext-1 gettext-2 \ xgettext-18 \ xgettext-appdata-1 \ xgettext-awk-1 xgettext-awk-2 \ + xgettext-awk-stackovfl-1 xgettext-awk-stackovfl-2 \ xgettext-c-2 xgettext-c-3 xgettext-c-4 xgettext-c-5 xgettext-c-6 \ xgettext-c-comment-1 xgettext-c-comment-2 xgettext-c-comment-3 \ xgettext-c-comment-4 xgettext-c-comment-5 xgettext-c-comment-6 \ diff --git a/gettext-tools/tests/xgettext-awk-stackovfl-1 b/gettext-tools/tests/xgettext-awk-stackovfl-1 new file mode 100755 index 000000000..fdd37c118 --- /dev/null +++ b/gettext-tools/tests/xgettext-awk-stackovfl-1 @@ -0,0 +1,63 @@ +#! /bin/sh +. "${srcdir=.}/init.sh"; path_prepend_ . ../src + +# Test awk support: stack overflow prevented by nesting depth check. + +cat < xg-a-so-1.awk +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +_"Hello!" +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +EOF + +: ${XGETTEXT=xgettext} +${XGETTEXT} --omit-header --no-location -d xg-a-so-1.tmp xg-a-so-1.awk || Exit 1 +LC_ALL=C tr -d '\r' < xg-a-so-1.tmp.po > xg-a-so-1.po || Exit 1 + +cat < xg-a-so-1.ok +msgid "Hello!" +msgstr "" +EOF + +: ${DIFF=diff} +${DIFF} xg-a-so-1.ok xg-a-so-1.po +result=$? + +exit $result diff --git a/gettext-tools/tests/xgettext-awk-stackovfl-2 b/gettext-tools/tests/xgettext-awk-stackovfl-2 new file mode 100755 index 000000000..8e1f75c3b --- /dev/null +++ b/gettext-tools/tests/xgettext-awk-stackovfl-2 @@ -0,0 +1,58 @@ +#! /bin/sh +. "${srcdir=.}/init.sh"; path_prepend_ . ../src + +# Test awk support: stack overflow prevented by nesting depth check. + +cat < xg-a-so-2.awk +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +( +_"Hello!" +) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +EOF + +: ${XGETTEXT=xgettext} +${XGETTEXT} --omit-header --no-location -d xg-a-so-2.tmp xg-a-so-2.awk 2>xg-a-so-2.err +result=$? +cat xg-a-so-2.err +test $result = 1 || Exit 1 + +exit 0