* 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.
/* 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>
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
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)))
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;
phase6_pushback_length = 0;
flag_context_list_table = flag_table;
+ nesting_depth = 0;
init_keywords ();
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 \
--- /dev/null
+#! /bin/sh
+. "${srcdir=.}/init.sh"; path_prepend_ . ../src
+
+# Test C support: stack overflow prevented by nesting depth check.
+
+cat <<EOF > 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 <<EOF > 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
--- /dev/null
+#! /bin/sh
+. "${srcdir=.}/init.sh"; path_prepend_ . ../src
+
+# Test C support: stack overflow prevented by nesting depth check.
+
+cat <<EOF > 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