From: Bruno Haible Date: Mon, 6 Mar 2023 17:54:00 +0000 (+0100) Subject: xgettext: In language C, avoid stack overflow. X-Git-Tag: v0.22~142 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a6636c9a310a50cef71710008a7ff4bd62fe2b31;p=thirdparty%2Fgettext.git 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. --- 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 @@ -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 (); diff --git a/gettext-tools/tests/Makefile.am b/gettext-tools/tests/Makefile.am index 92d28c479..b135cea5e 100644 --- a/gettext-tools/tests/Makefile.am +++ b/gettext-tools/tests/Makefile.am @@ -91,6 +91,7 @@ TESTS = gettext-1 gettext-2 \ xgettext-c-format-4 xgettext-c-format-5 \ xgettext-c-ctxt-1 xgettext-c-ctxt-2 xgettext-c-ctxt-3 \ xgettext-c-c++-1 xgettext-c-c++-2 \ + xgettext-c-stackovfl-1 xgettext-c-stackovfl-2 \ xgettext-csharp-1 xgettext-csharp-2 xgettext-csharp-3 \ xgettext-csharp-4 xgettext-csharp-5 xgettext-csharp-6 \ xgettext-csharp-7 xgettext-csharp-8 \ diff --git a/gettext-tools/tests/xgettext-c-stackovfl-1 b/gettext-tools/tests/xgettext-c-stackovfl-1 new file mode 100755 index 000000000..98060c45e --- /dev/null +++ b/gettext-tools/tests/xgettext-c-stackovfl-1 @@ -0,0 +1,63 @@ +#! /bin/sh +. "${srcdir=.}/init.sh"; path_prepend_ . ../src + +# Test C support: stack overflow prevented by nesting depth check. + +cat < xg-c-so-1.c +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +((((((((((((((((((((((((((((((((((((((((((((((((( +gettext("Hello!") +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +))))))))))))))))))))))))))))))))))))))))))))))))) +EOF + +: ${XGETTEXT=xgettext} +${XGETTEXT} --omit-header --no-location -d xg-c-so-1.tmp xg-c-so-1.c || Exit 1 +LC_ALL=C tr -d '\r' < xg-c-so-1.tmp.po > xg-c-so-1.po || Exit 1 + +cat < xg-c-so-1.ok +msgid "Hello!" +msgstr "" +EOF + +: ${DIFF=diff} +${DIFF} xg-c-so-1.ok xg-c-so-1.po +result=$? + +exit $result diff --git a/gettext-tools/tests/xgettext-c-stackovfl-2 b/gettext-tools/tests/xgettext-c-stackovfl-2 new file mode 100755 index 000000000..7f37795df --- /dev/null +++ b/gettext-tools/tests/xgettext-c-stackovfl-2 @@ -0,0 +1,56 @@ +#! /bin/sh +. "${srcdir=.}/init.sh"; path_prepend_ . ../src + +# Test C support: stack overflow prevented by nesting depth check. + +cat < xg-c-so-2.c +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +gettext("Hello!") +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +EOF + +: ${XGETTEXT=xgettext} +${XGETTEXT} --omit-header --no-location -d xg-c-so-2.tmp xg-c-so-2.c 2>xg-c-so-2.err +result=$? +cat xg-c-so-2.err +test $result = 1 || Exit 1 + +exit 0