diff options
author | Bruno Haible <bruno@clisp.org> | 2023-03-07 17:16:34 +0100 |
---|---|---|
committer | Bruno Haible <bruno@clisp.org> | 2023-03-09 11:38:12 +0100 |
commit | 8a5b80ae39720ffe41da206ddfa62c5fcc2f3777 (patch) | |
tree | 580eb5400e13f50631d05dfb750af53da173de7c | |
parent | 25109a5f967b85c16c17616154f87ab4175b42cc (diff) | |
download | gettext-8a5b80ae39720ffe41da206ddfa62c5fcc2f3777.tar.gz |
xgettext: In language Lisp, avoid stack overflow.
* 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.
-rw-r--r-- | gettext-tools/src/x-lisp.c | 27 | ||||
-rw-r--r-- | gettext-tools/tests/Makefile.am | 1 | ||||
-rwxr-xr-x | gettext-tools/tests/xgettext-lisp-stackovfl-1 | 63 | ||||
-rwxr-xr-x | gettext-tools/tests/xgettext-lisp-stackovfl-2 | 56 |
4 files changed, 146 insertions, 1 deletions
diff --git a/gettext-tools/src/x-lisp.c b/gettext-tools/src/x-lisp.c index 0137ffdfb..60e83f33b 100644 --- a/gettext-tools/src/x-lisp.c +++ b/gettext-tools/src/x-lisp.c @@ -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 (); diff --git a/gettext-tools/tests/Makefile.am b/gettext-tools/tests/Makefile.am index cb4d58cc0..b4e9a2581 100644 --- a/gettext-tools/tests/Makefile.am +++ b/gettext-tools/tests/Makefile.am @@ -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 index 000000000..fe7635ec4 --- /dev/null +++ b/gettext-tools/tests/xgettext-lisp-stackovfl-1 @@ -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 index 000000000..b025a92cd --- /dev/null +++ b/gettext-tools/tests/xgettext-lisp-stackovfl-2 @@ -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 |