From: Bruno Haible Date: Sun, 1 Oct 2023 18:18:06 +0000 (+0200) Subject: tests: Check against stack overflow during plural expression evaluation. X-Git-Tag: v0.23~325 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=021348871a22041cd9d2625ab958dd4808fd282d;p=thirdparty%2Fgettext.git tests: Check against stack overflow during plural expression evaluation. * gettext-tools/tests/plural-1: Add comment. * gettext-tools/tests/plural-2: Add comment. * gettext-tools/tests/plural-3: New file. * gettext-tools/tests/Makefile.am (TESTS): Add it. --- diff --git a/gettext-tools/tests/Makefile.am b/gettext-tools/tests/Makefile.am index 86de8b458..328381101 100644 --- a/gettext-tools/tests/Makefile.am +++ b/gettext-tools/tests/Makefile.am @@ -200,7 +200,7 @@ TESTS = gettext-1 gettext-2 \ format-sh-1 format-sh-2 \ format-tcl-1 format-tcl-2 \ format-ycp-1 format-ycp-2 \ - plural-1 plural-2 \ + plural-1 plural-2 plural-3 \ gettextpo-1 sentence-1 \ lang-po \ lang-c lang-c++ lang-c++20 lang-objc \ diff --git a/gettext-tools/tests/plural-1 b/gettext-tools/tests/plural-1 index 84ad7d2bd..2f6602c57 100755 --- a/gettext-tools/tests/plural-1 +++ b/gettext-tools/tests/plural-1 @@ -1,6 +1,8 @@ #! /bin/sh . "${srcdir=.}/init.sh"; path_prepend_ . ../src +# Test that ngettext() does plural form handling, with a default plural rule. + : ${XGETTEXT=xgettext} ${XGETTEXT} -o plural-1-cake.tmp --omit-header --no-location "$wabs_srcdir"/plural-1-prg.c || Exit 1 LC_ALL=C tr -d '\r' < plural-1-cake.tmp > plural-1-cake.pot || Exit 1 diff --git a/gettext-tools/tests/plural-2 b/gettext-tools/tests/plural-2 index 983f20772..e9ea320ab 100755 --- a/gettext-tools/tests/plural-2 +++ b/gettext-tools/tests/plural-2 @@ -1,6 +1,8 @@ #! /bin/sh . "${srcdir=.}/init.sh"; path_prepend_ . ../src +# Test that ngettext() selects the correct plural form, for 0 <= n < 200. + : ${MSGFMT=msgfmt} test -d plural-2-dir || mkdir plural-2-dir test -d plural-2-dir/ll || mkdir plural-2-dir/ll diff --git a/gettext-tools/tests/plural-3 b/gettext-tools/tests/plural-3 new file mode 100644 index 000000000..4a4fd3fe6 --- /dev/null +++ b/gettext-tools/tests/plural-3 @@ -0,0 +1,85 @@ +#! /bin/sh +. "${srcdir=.}/init.sh"; path_prepend_ . ../src + +# Test that ngettext() does not crash when the plural form rule leads to +# a stack overflow. + +# In fact, the stack overflow is caught by the bison parser (plural.c, +# macro YYMAXDEPTH = 10000). Since the same bison parser is used by +# glibc, libintl, and 'msgfmt -c', we can observe the behaviour by using +# the msgfmt option '-c'. + +test -d plural-3-dir || mkdir plural-3-dir +test -d plural-3-dir/ll || mkdir plural-3-dir/ll +test -d plural-3-dir/ll/LC_MESSAGES || mkdir plural-3-dir/ll/LC_MESSAGES + +# This one does not exceed YYMAXDEPTH, i.e. the parser succeeds, and we can +# test the stack consumption of plural_eval(). + +cat > plural-3-ll.po < dataout +test $? = 0 || Exit 1 +test y = "`cat dataout`" || Exit 1 + +LANGUAGE= TEXTDOMAIN=plural TEXTDOMAINDIR=plural-3-dir \ +$NGETTEXT --env LC_ALL=ll --thread X Y 42 > thread-dataout +test $? = 0 || Exit 1 +test y = "`cat thread-dataout`" || Exit 1 + +# This one is large enough that YYMAXDEPTH is exceeded, i.e. the parser fails. + +cat > plural-3-ll.po < parserfail-dataout +test $? = 0 || Exit 1 +test y = "`cat parserfail-dataout`" || Exit 1 + +LANGUAGE= TEXTDOMAIN=plural TEXTDOMAINDIR=plural-3-dir \ +$NGETTEXT --env LC_ALL=ll --thread X Y 42 > thread-parserfail-dataout +test $? = 0 || Exit 1 +test y = "`cat thread-parserfail-dataout`" || Exit 1 + +Exit 0