2 # This file is part of the GNU m4 testsuite
3 # Copyright (C) 2000, 2003, 2007-2014, 2016-2017, 2020-2021 Free
4 # Software Foundation, Inc.
6 # This file is part of GNU M4.
8 # GNU M4 is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # GNU M4 is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <https://www.gnu.org/licenses/>.
21 # Script to verify that stack overflow is diagnosed properly when
22 # there is infinite macro call nesting, provided the OS supports it.
26 # Skip this test if -L defaults to 1024 instead of 0, as that is our
27 # indicator that the OS does not support stack overflow detection.
28 ("$m4" --help | grep 'nesting.*\[0\]') >/dev/null 2>&1 \
30 echo "$0: skipping test, no stack overflow support detected in $m4"
34 # On some systems the ulimit command is available in ksh or bash but not sh
35 (exec 2>/dev/null; ulimit -Ss 300) || {
36 for altshell in bash bsh ksh zsh ; do
37 if (exec >/dev/null 2>&1; $altshell -c 'ulimit -Ss 300') && test -z "$2"
39 echo "Using $altshell because it supports ulimit"
40 exec $altshell "$0" "$@" running-with-$altshell
47 trap 'st=$?; rm -rf "$tmpdir" && exit $st' 0
48 trap '(exit $?); exit $?' 1 2 3 15
50 # Create a temporary subdirectory $tmpdir in $TMPDIR (default /tmp).
51 # Use mktemp if possible; otherwise fall back on mkdir,
52 # with $RANDOM to make collisions less likely.
56 (umask 077 && mktemp -d "$TMPDIR/m4stk-XXXXXX") 2>/dev/null
58 test -n "$tmpdir" && test -d "$tmpdir"
60 tmpdir=$TMPDIR/m4stk-$$-$RANDOM
61 (umask 077 && mkdir "$tmpdir")
63 tmpfile="$tmpdir"/m4.out
65 # Limit the stack size if the shell we are running permits it
66 if (exec 2>/dev/null; ulimit -Ss 300)
69 echo "Stack soft limit set to `ulimit -s`K";
71 echo "Can't reset stack limit - this may take a while..."
74 # Induce stack overflow.
75 echo 'define(a,a(a))a' | "$m4" > "$tmpfile" 2>&1
79 if test $result -eq 0 ; then
80 echo "Failure - $m4 did not abort"
82 # See if stack overflow was diagnosed.
83 case `cat "$tmpfile"` in
85 case `echo "$tmpdir"/*` in
89 *) echo "Failure - $m4 created unexpected core dump"
92 *) echo "Failure - $m4 aborted unexpectedly";
98 { echo "Output from $m4:"; cat $tmpfile; }