]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
xgettext: In language librep, avoid stack overflow.
authorBruno Haible <bruno@clisp.org>
Tue, 7 Mar 2023 23:02:07 +0000 (00:02 +0100)
committerBruno Haible <bruno@clisp.org>
Thu, 9 Mar 2023 10:40:25 +0000 (11:40 +0100)
* gettext-tools/src/x-librep.c: Include error-progname.h.
(MAX_NESTING_DEPTH): New macro.
(nesting_depth): New variable.
(read_object): Increase nesting_depth before calling read_object recursively.
Check nesting_depth.
(extract_librep): Initialize nesting_depth.
* gettext-tools/tests/xgettext-librep-stackovfl-1: New file.
* gettext-tools/tests/xgettext-librep-stackovfl-2: New file.
* gettext-tools/tests/Makefile.am (TESTS): Add them.

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

index d694a0b5184edc1f3e439530abc4c8363619681f..734d3880715e65f36335e262c6337298e3bdc165 100644 (file)
@@ -1,5 +1,5 @@
 /* xgettext librep backend.
-   Copyright (C) 2001-2003, 2005-2009, 2018-2020 Free Software Foundation, Inc.
+   Copyright (C) 2001-2003, 2005-2009, 2018-2023 Free Software Foundation, Inc.
 
    This file was written by Bruno Haible <haible@clisp.cons.org>, 2001.
 
@@ -40,6 +40,7 @@
 #include "xg-arglist-parser.h"
 #include "xg-message.h"
 #include "error.h"
+#include "error-progname.h"
 #include "xalloc.h"
 #include "mem-hash-map.h"
 #include "gettext.h"
@@ -507,9 +508,18 @@ string_of_object (const struct object *op)
   return str;
 }
 
+
 /* Context lookup table.  */
 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;
+
+
 /* Returns the character represented by an escape sequence.  */
 static int
 do_getc_escaped (int c)
@@ -590,6 +600,12 @@ do_getc_escaped (int c)
 static void
 read_object (struct object *op, flag_context_ty outer_context)
 {
+  if (nesting_depth > MAX_NESTING_DEPTH)
+    {
+      error_with_progname = false;
+      error (EXIT_FAILURE, 0, _("%s:%d: error: too deeply nested objects"),
+             logical_file_name, line_number);
+    }
   for (;;)
     {
       int c;
@@ -633,7 +649,9 @@ read_object (struct object *op, flag_context_ty outer_context)
                                        flag_context_list_iterator_advance (
                                          &context_iter));
 
+                ++nesting_depth;
                 read_object (&inner, inner_context);
+                nesting_depth--;
 
                 /* Recognize end of list.  */
                 if (inner.type == t_close)
@@ -715,7 +733,9 @@ read_object (struct object *op, flag_context_ty outer_context)
               {
                 struct object inner;
 
+                ++nesting_depth;
                 read_object (&inner, null_context);
+                nesting_depth--;
 
                 /* Recognize end of vector.  */
                 if (inner.type == t_close)
@@ -759,7 +779,9 @@ read_object (struct object *op, flag_context_ty outer_context)
           {
             struct object inner;
 
+            ++nesting_depth;
             read_object (&inner, null_context);
+            nesting_depth--;
 
             /* Dots and EOF are not allowed here.  But be tolerant.  */
 
@@ -914,7 +936,9 @@ read_object (struct object *op, flag_context_ty outer_context)
             case ':':
               {
                 struct object inner;
+                ++nesting_depth;
                 read_object (&inner, null_context);
+                nesting_depth--;
                 /* Dots and EOF are not allowed here.
                    But be tolerant.  */
                 free_object (&inner);
@@ -928,7 +952,9 @@ read_object (struct object *op, flag_context_ty outer_context)
               {
                 struct object inner;
                 do_ungetc (c);
+                ++nesting_depth;
                 read_object (&inner, null_context);
+                nesting_depth--;
                 /* Dots and EOF are not allowed here.
                    But be tolerant.  */
                 free_object (&inner);
@@ -1115,6 +1141,7 @@ extract_librep (FILE *f,
   last_non_comment_line = -1;
 
   flag_context_list_table = flag_table;
+  nesting_depth = 0;
 
   init_keywords ();
 
index d696f7799e1b9085d68e752b2b7fc4be9f3f1368..505aa0616722215b8577c95dbdda3200f6838b25 100644 (file)
@@ -117,6 +117,7 @@ TESTS = gettext-1 gettext-2 \
        xgettext-javascript-stackovfl-5 xgettext-javascript-stackovfl-6 \
        xgettext-javascript-stackovfl-7 xgettext-javascript-stackovfl-8 \
        xgettext-librep-1 xgettext-librep-2 \
+       xgettext-librep-stackovfl-1 xgettext-librep-stackovfl-2 \
        xgettext-lisp-1 xgettext-lisp-2 \
        xgettext-lisp-stackovfl-1 xgettext-lisp-stackovfl-2 \
        xgettext-lua-1 xgettext-lua-2 \
diff --git a/gettext-tools/tests/xgettext-librep-stackovfl-1 b/gettext-tools/tests/xgettext-librep-stackovfl-1
new file mode 100755 (executable)
index 0000000..11fe30a
--- /dev/null
@@ -0,0 +1,63 @@
+#! /bin/sh
+. "${srcdir=.}/init.sh"; path_prepend_ . ../src
+
+# Test librep support: stack overflow prevented by nesting depth check.
+
+cat <<EOF > xg-lr-so-1.jl
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+(((((((((((((((((((((((((((((((((((((((((((((((((
+(_ "Hello!")
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+)))))))))))))))))))))))))))))))))))))))))))))))))
+EOF
+
+: ${XGETTEXT=xgettext}
+${XGETTEXT} --omit-header --no-location -d xg-lr-so-1.tmp xg-lr-so-1.jl || Exit 1
+LC_ALL=C tr -d '\r' < xg-lr-so-1.tmp.po > xg-lr-so-1.po || Exit 1
+
+cat <<EOF > xg-lr-so-1.ok
+msgid "Hello!"
+msgstr ""
+EOF
+
+: ${DIFF=diff}
+${DIFF} xg-lr-so-1.ok xg-lr-so-1.po
+result=$?
+
+exit $result
diff --git a/gettext-tools/tests/xgettext-librep-stackovfl-2 b/gettext-tools/tests/xgettext-librep-stackovfl-2
new file mode 100755 (executable)
index 0000000..24c6434
--- /dev/null
@@ -0,0 +1,56 @@
+#! /bin/sh
+. "${srcdir=.}/init.sh"; path_prepend_ . ../src
+
+# Test librep support: stack overflow prevented by nesting depth check.
+
+cat <<EOF > xg-lr-so-2.jl
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+(_ "Hello!")
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+EOF
+
+: ${XGETTEXT=xgettext}
+${XGETTEXT} --omit-header --no-location -d xg-lr-so-2.tmp xg-lr-so-2.jl 2>xg-lr-so-2.err
+result=$?
+cat xg-lr-so-2.err
+test $result = 1 || Exit 1
+
+exit 0