Imported Upstream version 1.4.16
[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-2011 Free Software Foundation, Inc.
4 #
5 # This file is part of GNU M4.
6 #
7 # GNU M4 is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # GNU M4 is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 # Script to verify that stack overflow is diagnosed properly when
21 # there is infinite macro call nesting, provided the OS supports it.
22
23 m4="$1"
24
25 # Skip this test if -L defaults to 1024 instead of 0, as that is our
26 # indicator that the OS does not support stack overflow detection.
27 ("$m4" --help | grep 'nesting.*\[0\]') >/dev/null 2>&1 \
28   || {
29        echo "$0: skipping test, no stack overflow support detected in $m4"
30        exit 77
31      }
32
33 # On some systems the ulimit command is available in ksh or bash but not sh
34 (exec 2>/dev/null; ulimit -Ss 300) || {
35   for altshell in bash bsh ksh zsh ; do
36     if (exec >/dev/null 2>&1; $altshell -c 'ulimit -Ss 300') && test -z "$2"
37     then
38       echo "Using $altshell because it supports ulimit"
39       exec $altshell "$0" "$@" running-with-$altshell
40       exit 1
41     fi
42   done
43 }
44
45 tmpdir=
46 trap 'st=$?; rm -rf "$tmpdir" && exit $st' 0
47 trap '(exit $?); exit $?' 1 2 3 15
48
49 # Create a temporary subdirectory $tmpdir in $TMPDIR (default /tmp).
50 # Use mktemp if possible; otherwise fall back on mkdir,
51 # with $RANDOM to make collisions less likely.
52 : ${TMPDIR=/tmp}
53 {
54   tmpdir=`
55     (umask 077 && mktemp -d "$TMPDIR/m4stk-XXXXXX") 2>/dev/null
56   ` &&
57   test -n "$tmpdir" && test -d "$tmpdir"
58 } || {
59   tmpdir=$TMPDIR/m4stk-$$-$RANDOM
60   (umask 077 && mkdir "$tmpdir")
61 } || exit $?
62 tmpfile="$tmpdir"/m4.out
63
64 # Limit the stack size if the shell we are running permits it
65 if (exec 2>/dev/null; ulimit -Ss 300)
66 then
67   ulimit -Ss 300
68   echo "Stack soft limit set to `ulimit -s`K";
69 else
70   echo "Can't reset stack limit - this may take a while..."
71 fi
72
73 # Induce stack overflow.
74 echo 'define(a,a(a))a' | "$m4" > "$tmpfile" 2>&1
75 result=$?
76
77 exitcode=1
78 if test $result -eq 0 ; then
79   echo "Failure - $m4 did not abort"
80 else
81   # See if stack overflow was diagnosed.
82   case `cat "$tmpfile"` in
83     *stack\ overflow*)
84       case `echo "$tmpdir"/*` in
85         $tmpfile)
86            echo "Pass"
87            exitcode=0 ;;
88         *) echo "Failure - $m4 created unexpected core dump"
89            ls -l "$tmpdir" ;;
90       esac ;;
91     *) echo "Failure - $m4 aborted unexpectedly";
92         ;;
93     esac
94 fi
95
96 test $exitcode = 0 ||
97     { echo "Output from $m4:"; cat $tmpfile; }
98
99 exit $exitcode