Imported Upstream version 1.4.19
[platform/upstream/m4.git] / checks / stackovf.test
1 #!/bin/sh
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.
5 #
6 # This file is part of GNU M4.
7 #
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.
12 #
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.
17 #
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/>.
20
21 # Script to verify that stack overflow is diagnosed properly when
22 # there is infinite macro call nesting, provided the OS supports it.
23
24 m4="$1"
25
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 \
29   || {
30        echo "$0: skipping test, no stack overflow support detected in $m4"
31        exit 77
32      }
33
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"
38     then
39       echo "Using $altshell because it supports ulimit"
40       exec $altshell "$0" "$@" running-with-$altshell
41       exit 1
42     fi
43   done
44 }
45
46 tmpdir=
47 trap 'st=$?; rm -rf "$tmpdir" && exit $st' 0
48 trap '(exit $?); exit $?' 1 2 3 15
49
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.
53 : ${TMPDIR=/tmp}
54 {
55   tmpdir=`
56     (umask 077 && mktemp -d "$TMPDIR/m4stk-XXXXXX") 2>/dev/null
57   ` &&
58   test -n "$tmpdir" && test -d "$tmpdir"
59 } || {
60   tmpdir=$TMPDIR/m4stk-$$-$RANDOM
61   (umask 077 && mkdir "$tmpdir")
62 } || exit $?
63 tmpfile="$tmpdir"/m4.out
64
65 # Limit the stack size if the shell we are running permits it
66 if (exec 2>/dev/null; ulimit -Ss 300)
67 then
68   ulimit -Ss 300
69   echo "Stack soft limit set to `ulimit -s`K";
70 else
71   echo "Can't reset stack limit - this may take a while..."
72 fi
73
74 # Induce stack overflow.
75 echo 'define(a,a(a))a' | "$m4" > "$tmpfile" 2>&1
76 result=$?
77
78 exitcode=1
79 if test $result -eq 0 ; then
80   echo "Failure - $m4 did not abort"
81 else
82   # See if stack overflow was diagnosed.
83   case `cat "$tmpfile"` in
84     *stack\ overflow*)
85       case `echo "$tmpdir"/*` in
86         $tmpfile)
87            echo "Pass"
88            exitcode=0 ;;
89         *) echo "Failure - $m4 created unexpected core dump"
90            ls -l "$tmpdir" ;;
91       esac ;;
92     *) echo "Failure - $m4 aborted unexpectedly";
93         ;;
94     esac
95 fi
96
97 test $exitcode = 0 ||
98     { echo "Output from $m4:"; cat $tmpfile; }
99
100 exit $exitcode