]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
xgettext: In language Java, avoid stack overflow.
authorBruno Haible <bruno@clisp.org>
Mon, 6 Mar 2023 20:49:02 +0000 (21:49 +0100)
committerBruno Haible <bruno@clisp.org>
Tue, 7 Mar 2023 15:59:30 +0000 (16:59 +0100)
* gettext-tools/src/x-java.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_java): Initialize paren_nesting_depth and brace_nesting_depth.
* gettext-tools/tests/xgettext-java-stackovfl-1: New file.
* gettext-tools/tests/xgettext-java-stackovfl-2: New file.
* gettext-tools/tests/xgettext-java-stackovfl-3: New file.
* gettext-tools/tests/xgettext-java-stackovfl-4: New file.
* gettext-tools/tests/Makefile.am (TESTS): Add them.

gettext-tools/src/x-java.c
gettext-tools/tests/Makefile.am
gettext-tools/tests/xgettext-java-stackovfl-1 [new file with mode: 0755]
gettext-tools/tests/xgettext-java-stackovfl-2 [new file with mode: 0755]
gettext-tools/tests/xgettext-java-stackovfl-3 [new file with mode: 0755]
gettext-tools/tests/xgettext-java-stackovfl-4 [new file with mode: 0755]

index e89ab95f477603d119ab6cee13d4c1c1011815eb..0e1e62387603ea41253da35868241bccc4eab821 100644 (file)
@@ -1,5 +1,5 @@
 /* xgettext Java backend.
-   Copyright (C) 2003, 2005-2009, 2018-2020 Free Software Foundation, Inc.
+   Copyright (C) 2003, 2005-2009, 2018-2023 Free Software Foundation, Inc.
    Written by Bruno Haible <bruno@clisp.org>, 2003.
 
    This program is free software: you can redistribute it and/or modify
@@ -1432,6 +1432,14 @@ x_java_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
@@ -1567,6 +1575,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,
@@ -1575,6 +1589,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;
@@ -1598,6 +1613,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)))
@@ -1605,6 +1626,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;
@@ -1707,6 +1729,8 @@ extract_java (FILE *f,
   phase6_last = token_type_eof;
 
   flag_context_list_table = flag_table;
+  paren_nesting_depth = 0;
+  brace_nesting_depth = 0;
 
   init_keywords ();
 
index 98a82ce683b016783c9fa1108f05d6aa83575589..c6dc15f6585a374f6c231d4bb725cfb87758e990 100644 (file)
@@ -103,6 +103,8 @@ TESTS = gettext-1 gettext-2 \
        xgettext-its-1 xgettext-its-2 \
        xgettext-java-1 xgettext-java-2 xgettext-java-3 xgettext-java-4 \
        xgettext-java-5 xgettext-java-6 xgettext-java-7 \
+       xgettext-java-stackovfl-1 xgettext-java-stackovfl-2 \
+       xgettext-java-stackovfl-3 xgettext-java-stackovfl-4 \
        xgettext-javascript-1 xgettext-javascript-2 xgettext-javascript-3 \
        xgettext-javascript-4 xgettext-javascript-5 xgettext-javascript-6 \
        xgettext-javascript-7 \
diff --git a/gettext-tools/tests/xgettext-java-stackovfl-1 b/gettext-tools/tests/xgettext-java-stackovfl-1
new file mode 100755 (executable)
index 0000000..3471ecc
--- /dev/null
@@ -0,0 +1,63 @@
+#! /bin/sh
+. "${srcdir=.}/init.sh"; path_prepend_ . ../src
+
+# Test Java support: stack overflow prevented by nesting depth check.
+
+cat <<EOF > xg-j-so-1.java
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+(((((((((((((((((((((((((((((((((((((((((((((((((
+bundle.gettext("Hello!")
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+)))))))))))))))))))))))))))))))))))))))))))))))))
+EOF
+
+: ${XGETTEXT=xgettext}
+${XGETTEXT} --omit-header --no-location -d xg-j-so-1.tmp xg-j-so-1.java || Exit 1
+LC_ALL=C tr -d '\r' < xg-j-so-1.tmp.po > xg-j-so-1.po || Exit 1
+
+cat <<EOF > xg-j-so-1.ok
+msgid "Hello!"
+msgstr ""
+EOF
+
+: ${DIFF=diff}
+${DIFF} xg-j-so-1.ok xg-j-so-1.po
+result=$?
+
+exit $result
diff --git a/gettext-tools/tests/xgettext-java-stackovfl-2 b/gettext-tools/tests/xgettext-java-stackovfl-2
new file mode 100755 (executable)
index 0000000..7939d83
--- /dev/null
@@ -0,0 +1,56 @@
+#! /bin/sh
+. "${srcdir=.}/init.sh"; path_prepend_ . ../src
+
+# Test Java support: stack overflow prevented by nesting depth check.
+
+cat <<EOF > xg-j-so-2.java
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+bundle.gettext("Hello!")
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+EOF
+
+: ${XGETTEXT=xgettext}
+${XGETTEXT} --omit-header --no-location -d xg-j-so-2.tmp xg-j-so-2.java 2>xg-j-so-2.err
+result=$?
+cat xg-j-so-2.err
+test $result = 1 || Exit 1
+
+exit 0
diff --git a/gettext-tools/tests/xgettext-java-stackovfl-3 b/gettext-tools/tests/xgettext-java-stackovfl-3
new file mode 100755 (executable)
index 0000000..ab39764
--- /dev/null
@@ -0,0 +1,63 @@
+#! /bin/sh
+. "${srcdir=.}/init.sh"; path_prepend_ . ../src
+
+# Test Java support: stack overflow prevented by nesting depth check.
+
+cat <<EOF > xg-j-so-3.java
+{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
+{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
+{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
+{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
+{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
+{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
+{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
+{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
+{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
+{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
+{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
+{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
+{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
+{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
+{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
+{{{{{{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}}}}}}}}}}}}}}}}}}}}}}}
+}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
+}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
+}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
+}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
+}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
+EOF
+
+: ${XGETTEXT=xgettext}
+${XGETTEXT} --omit-header --no-location -d xg-j-so-3.tmp xg-j-so-3.java || Exit 1
+LC_ALL=C tr -d '\r' < xg-j-so-3.tmp.po > xg-j-so-3.po || Exit 1
+
+cat <<EOF > xg-j-so-3.ok
+msgid "Hello!"
+msgstr ""
+EOF
+
+: ${DIFF=diff}
+${DIFF} xg-j-so-3.ok xg-j-so-3.po
+result=$?
+
+exit $result
diff --git a/gettext-tools/tests/xgettext-java-stackovfl-4 b/gettext-tools/tests/xgettext-java-stackovfl-4
new file mode 100755 (executable)
index 0000000..63002c3
--- /dev/null
@@ -0,0 +1,58 @@
+#! /bin/sh
+. "${srcdir=.}/init.sh"; path_prepend_ . ../src
+
+# Test Java support: stack overflow prevented by nesting depth check.
+
+cat <<EOF > xg-j-so-4.java
+{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
+{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
+{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
+{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
+{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
+{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
+{{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