]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
xgettext: In language Lisp, avoid stack overflow.
authorBruno Haible <bruno@clisp.org>
Tue, 7 Mar 2023 16:16:34 +0000 (17:16 +0100)
committerBruno Haible <bruno@clisp.org>
Thu, 9 Mar 2023 10:38:12 +0000 (11:38 +0100)
* gettext-tools/src/x-lisp.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_lisp): Initialize nesting_depth.
* gettext-tools/tests/xgettext-lisp-stackovfl-1: New file.
* gettext-tools/tests/xgettext-lisp-stackovfl-2: New file.
* gettext-tools/tests/Makefile.am (TESTS): Add them.

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

index 0137ffdfb383c661e7e5fde9a99bd2f3b977d911..60e83f33b3ba4eb298985e3b6a38080d9dc20697 100644 (file)
@@ -1,5 +1,5 @@
 /* xgettext Lisp 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.
 
@@ -39,6 +39,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"
@@ -926,13 +927,28 @@ 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;
+
+
 /* Read the next object.  */
 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 (;;)
     {
       struct char_syntax curr;
@@ -1025,7 +1041,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)
@@ -1134,7 +1152,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.  */
 
@@ -1242,7 +1262,9 @@ read_object (struct object *op, flag_context_ty outer_context)
                   case 'S': case 's':
                     {
                       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);
@@ -1360,7 +1382,9 @@ read_object (struct object *op, flag_context_ty outer_context)
                     /* Simply assume every feature expression is true.  */
                     {
                       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);
@@ -1406,6 +1430,7 @@ extract_lisp (FILE *f,
   last_non_comment_line = -1;
 
   flag_context_list_table = flag_table;
+  nesting_depth = 0;
 
   init_keywords ();
 
index cb4d58cc0fb8ea75a95f751a2de802ccf2b5a2ab..b4e9a25818e69c7500db34318198f90cf838d7c3 100644 (file)
@@ -116,6 +116,7 @@ TESTS = gettext-1 gettext-2 \
        xgettext-javascript-stackovfl-7 xgettext-javascript-stackovfl-8 \
        xgettext-librep-1 xgettext-librep-2 \
        xgettext-lisp-1 xgettext-lisp-2 \
+       xgettext-lisp-stackovfl-1 xgettext-lisp-stackovfl-2 \
        xgettext-lua-1 xgettext-lua-2 \
        xgettext-objc-1 xgettext-objc-2 \
        xgettext-perl-1 xgettext-perl-2 xgettext-perl-3 xgettext-perl-4 \
diff --git a/gettext-tools/tests/xgettext-lisp-stackovfl-1 b/gettext-tools/tests/xgettext-lisp-stackovfl-1
new file mode 100755 (executable)
index 0000000..fe7635e
--- /dev/null
@@ -0,0 +1,63 @@
+#! /bin/sh
+. "${srcdir=.}/init.sh"; path_prepend_ . ../src
+
+# Test Lisp support: stack overflow prevented by nesting depth check.
+
+cat <<EOF > xg-ls-so-1.lisp
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+(((((((((((((((((((((((((((((((((((((((((((((((((
+(gettext "Hello!")
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+)))))))))))))))))))))))))))))))))))))))))))))))))
+EOF
+
+: ${XGETTEXT=xgettext}
+${XGETTEXT} --omit-header --no-location -d xg-ls-so-1.tmp xg-ls-so-1.lisp || Exit 1
+LC_ALL=C tr -d '\r' < xg-ls-so-1.tmp.po > xg-ls-so-1.po || Exit 1
+
+cat <<EOF > xg-ls-so-1.ok
+msgid "Hello!"
+msgstr ""
+EOF
+
+: ${DIFF=diff}
+${DIFF} xg-ls-so-1.ok xg-ls-so-1.po
+result=$?
+
+exit $result
diff --git a/gettext-tools/tests/xgettext-lisp-stackovfl-2 b/gettext-tools/tests/xgettext-lisp-stackovfl-2
new file mode 100755 (executable)
index 0000000..b025a92
--- /dev/null
@@ -0,0 +1,56 @@
+#! /bin/sh
+. "${srcdir=.}/init.sh"; path_prepend_ . ../src
+
+# Test Lisp support: stack overflow prevented by nesting depth check.
+
+cat <<EOF > xg-ls-so-2.lisp
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+((((((((((((((((((((((((((((((((((((((((((((((((((
+(gettext "Hello!")
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+))))))))))))))))))))))))))))))))))))))))))))))))))
+EOF
+
+: ${XGETTEXT=xgettext}
+${XGETTEXT} --omit-header --no-location -d xg-ls-so-2.tmp xg-ls-so-2.lisp 2>xg-ls-so-2.err
+result=$?
+cat xg-ls-so-2.err
+test $result = 1 || Exit 1
+
+exit 0