From: Bruno Haible Date: Tue, 7 Mar 2023 23:02:07 +0000 (+0100) Subject: xgettext: In language librep, avoid stack overflow. X-Git-Tag: v0.22~133 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=793f539ef3201392ae5e7649f369f97285fbbfe4;p=thirdparty%2Fgettext.git xgettext: In language librep, avoid stack overflow. * 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. --- diff --git a/gettext-tools/src/x-librep.c b/gettext-tools/src/x-librep.c index d694a0b51..734d38807 100644 --- a/gettext-tools/src/x-librep.c +++ b/gettext-tools/src/x-librep.c @@ -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 , 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 (); diff --git a/gettext-tools/tests/Makefile.am b/gettext-tools/tests/Makefile.am index d696f7799..505aa0616 100644 --- a/gettext-tools/tests/Makefile.am +++ b/gettext-tools/tests/Makefile.am @@ -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 index 000000000..11fe30aad --- /dev/null +++ b/gettext-tools/tests/xgettext-librep-stackovfl-1 @@ -0,0 +1,63 @@ +#! /bin/sh +. "${srcdir=.}/init.sh"; path_prepend_ . ../src + +# Test librep support: stack overflow prevented by nesting depth check. + +cat < 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 < 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 index 000000000..24c643496 --- /dev/null +++ b/gettext-tools/tests/xgettext-librep-stackovfl-2 @@ -0,0 +1,56 @@ +#! /bin/sh +. "${srcdir=.}/init.sh"; path_prepend_ . ../src + +# Test librep support: stack overflow prevented by nesting depth check. + +cat < 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