From: Bruno Haible Date: Mon, 6 Mar 2023 21:21:03 +0000 (+0100) Subject: xgettext: In language C#, avoid stack overflow. X-Git-Tag: v0.22~139 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5355816c16910fbaa41f1eb4e128114672aa7571;p=thirdparty%2Fgettext.git xgettext: In language C#, avoid stack overflow. * gettext-tools/src/x-csharp.c (MAX_NESTING_DEPTH): New macro. (paren_nesting_depth, brace_nesting_depth): New variables. (extract_parenthesized): Increase and check paren_nesting_depth or brace_nesting_depth before calling extract_parenthesized recursively. (extract_csharp): Initialize paren_nesting_depth and brace_nesting_depth. * gettext-tools/tests/xgettext-csharp-stackovfl-1: New file. * gettext-tools/tests/xgettext-csharp-stackovfl-2: New file. * gettext-tools/tests/xgettext-csharp-stackovfl-3: New file. * gettext-tools/tests/xgettext-csharp-stackovfl-4: New file. * gettext-tools/tests/Makefile.am (TESTS): Add them. --- diff --git a/gettext-tools/src/x-csharp.c b/gettext-tools/src/x-csharp.c index 13d4a5310..44cc774e3 100644 --- a/gettext-tools/src/x-csharp.c +++ b/gettext-tools/src/x-csharp.c @@ -1,5 +1,5 @@ /* xgettext C# backend. - Copyright (C) 2003-2009, 2011, 2014, 2018-2020 Free Software Foundation, Inc. + Copyright (C) 2003-2009, 2011, 2014, 2018-2023 Free Software Foundation, Inc. Written by Bruno Haible , 2003. This program is free software: you can redistribute it and/or modify @@ -1794,6 +1794,14 @@ x_csharp_unlex (token_ty *tp) static flag_context_list_table_ty *flag_context_list_table; +/* Maximum supported nesting depth. */ +#define MAX_NESTING_DEPTH 1000 + +/* Current nesting depths. */ +static int paren_nesting_depth; +static int brace_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 @@ -1929,6 +1937,12 @@ extract_parenthesized (message_list_ty *mlp, token_type_ty terminator, } case token_type_lparen: + if (++paren_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, token_type_rparen, inner_context, next_context_iter, arglist_parser_alloc (mlp, @@ -1937,6 +1951,7 @@ extract_parenthesized (message_list_ty *mlp, token_type_ty terminator, arglist_parser_done (argparser, arg); return true; } + paren_nesting_depth--; next_context_iter = null_context_list_iterator; state = 0; continue; @@ -1960,6 +1975,12 @@ extract_parenthesized (message_list_ty *mlp, token_type_ty terminator, continue; case token_type_lbrace: + if (++brace_nesting_depth > MAX_NESTING_DEPTH) + { + error_with_progname = false; + error (EXIT_FAILURE, 0, _("%s:%d: error: too many open braces"), + logical_file_name, line_number); + } if (extract_parenthesized (mlp, token_type_rbrace, null_context, null_context_list_iterator, arglist_parser_alloc (mlp, NULL))) @@ -1967,6 +1988,7 @@ extract_parenthesized (message_list_ty *mlp, token_type_ty terminator, arglist_parser_done (argparser, arg); return true; } + brace_nesting_depth--; next_context_iter = null_context_list_iterator; state = 0; continue; @@ -2075,6 +2097,8 @@ extract_csharp (FILE *f, phase7_pushback_length = 0; flag_context_list_table = flag_table; + paren_nesting_depth = 0; + brace_nesting_depth = 0; init_keywords (); diff --git a/gettext-tools/tests/Makefile.am b/gettext-tools/tests/Makefile.am index c6dc15f65..bac8bc63b 100644 --- a/gettext-tools/tests/Makefile.am +++ b/gettext-tools/tests/Makefile.am @@ -95,6 +95,8 @@ TESTS = gettext-1 gettext-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 \ + xgettext-csharp-stackovfl-1 xgettext-csharp-stackovfl-2 \ + xgettext-csharp-stackovfl-3 xgettext-csharp-stackovfl-4 \ xgettext-desktop-1 xgettext-desktop-2 \ xgettext-elisp-1 xgettext-elisp-2 \ xgettext-glade-1 xgettext-glade-2 xgettext-glade-3 xgettext-glade-4 \ diff --git a/gettext-tools/tests/xgettext-csharp-stackovfl-1 b/gettext-tools/tests/xgettext-csharp-stackovfl-1 new file mode 100755 index 000000000..9bdb27e53 --- /dev/null +++ b/gettext-tools/tests/xgettext-csharp-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-cs-so-1.cs +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +((((((((((((((((((((((((((((((((((((((((((((((((( +rm.GetString("Hello!") +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +))))))))))))))))))))))))))))))))))))))))))))))))) +EOF + +: ${XGETTEXT=xgettext} +${XGETTEXT} --omit-header --no-location -d xg-cs-so-1.tmp xg-cs-so-1.cs || Exit 1 +LC_ALL=C tr -d '\r' < xg-cs-so-1.tmp.po > xg-cs-so-1.po || Exit 1 + +cat < xg-cs-so-1.ok +msgid "Hello!" +msgstr "" +EOF + +: ${DIFF=diff} +${DIFF} xg-cs-so-1.ok xg-cs-so-1.po +result=$? + +exit $result diff --git a/gettext-tools/tests/xgettext-csharp-stackovfl-2 b/gettext-tools/tests/xgettext-csharp-stackovfl-2 new file mode 100755 index 000000000..6fb475b7d --- /dev/null +++ b/gettext-tools/tests/xgettext-csharp-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-cs-so-2.cs +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +(((((((((((((((((((((((((((((((((((((((((((((((((( +rm.GetString("Hello!") +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +)))))))))))))))))))))))))))))))))))))))))))))))))) +EOF + +: ${XGETTEXT=xgettext} +${XGETTEXT} --omit-header --no-location -d xg-cs-so-2.tmp xg-cs-so-2.cs 2>xg-cs-so-2.err +result=$? +cat xg-cs-so-2.err +test $result = 1 || Exit 1 + +exit 0 diff --git a/gettext-tools/tests/xgettext-csharp-stackovfl-3 b/gettext-tools/tests/xgettext-csharp-stackovfl-3 new file mode 100755 index 000000000..f1c4c7413 --- /dev/null +++ b/gettext-tools/tests/xgettext-csharp-stackovfl-3 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}}}}}}}}}}}}}}}}}}} +}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}} +}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}} +EOF + +: ${XGETTEXT=xgettext} +${XGETTEXT} --omit-header --no-location -d xg-cs-so-3.tmp xg-cs-so-3.cs || Exit 1 +LC_ALL=C tr -d '\r' < xg-cs-so-3.tmp.po > xg-cs-so-3.po || Exit 1 + +cat < xg-cs-so-3.ok +msgid "Hello!" +msgstr "" +EOF + +: ${DIFF=diff} +${DIFF} xg-cs-so-3.ok xg-cs-so-3.po +result=$? + +exit $result diff --git a/gettext-tools/tests/xgettext-csharp-stackovfl-4 b/gettext-tools/tests/xgettext-csharp-stackovfl-4 new file mode 100755 index 000000000..629390809 --- /dev/null +++ b/gettext-tools/tests/xgettext-csharp-stackovfl-4 @@ -0,0 +1,58 @@ +#! /bin/sh +. "${srcdir=.}/init.sh"; path_prepend_ . ../src + +# Test C# support: stack overflow prevented by nesting depth check. + +cat < xg-cs-so-4.cs +{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{ +{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{ +{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{ +{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{ +{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{ +{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{ +{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{ +{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{ +{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{ +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