Fix 'm4 -F file -t undefined'.
[m4/ericb.git] / examples / stackovf.sh
blob8916afc3ba2127f979a4dc7c8252081819c853df
1 #!/bin/sh
3 # Script to verify that stack overflow is diagnosed properly when
4 # there is infinite macro call nesting.
5 # (causes coredump in m4-1.0.3)
7 # On some systems the ulimit command is available in ksh or bash but not sh
8 (exec 2>/dev/null; ulimit -HSs 300) || {
9 for altshell in bash bsh ksh ; do
10 if (exec >/dev/null 2>&1; $altshell -c 'ulimit -HSs 300') &&
11 test -z "$1"
12 then
13 echo "Using $altshell because it supports ulimit"
14 exec $altshell $0 running-with-$altshell
15 exit 9
17 done
20 PATH=.:..:$PATH; export PATH;
21 M4=m4
22 type $M4
24 tmpfile=`tempfile 2> /dev/null` || tmpfile=/tmp/t.$$
25 trap 'rm -f $tmpfile; exit 1' 1 2 3 15
27 rm -f core
28 perl -e '
29 # Generate nested define sequence
30 $max=1000000;
31 for ($i=0; $i<$max; $i++) {
32 print "define(X$i,\n";
34 for ($i=$max-1; $i>=0; $i--) {
35 print "body with substance no. $i)dnl\n"
37 ' | \
39 # Limit the stack size if the shell we are running permits it
40 if (exec 2>/dev/null; ulimit -HSs 50)
41 then
42 (exec >/dev/null 2>&1; ulimit -v) && ulimitdashv=ok
43 ulimit -HSs 50
44 #ulimit -HSd 8000
45 #test -n "$ulimitdashv" && ulimit -HSv 8000
46 echo "Stack limit is `ulimit -s`K";
47 echo "Heap limit is `ulimit -d`K";
48 test -n "$ulimitdashv" &&
49 echo "VMem limit is `ulimit -v`K";
50 else
51 echo "Can't reset stack limit - this may take a while..."
53 $M4 -L999999999 > $tmpfile 2>&1
55 result=$?
57 exitcode=1
58 if test $result -eq 0 ; then
59 echo "TEST DID NOT WORK - m4 did not abort. Output:"
60 else
61 # See if stack overflow was diagnosed
62 case "`cat $tmpfile`" in
63 *overflow*)
64 echo "Test succeeded.";
65 exitcode=0
67 *ut*of*emory*)
68 echo "*** Test is INCONCLUSIVE (ran out of heap before stack overflow)";
70 *) echo "*** Test FAILED. $M4 aborted unexpectedly. Output:";
72 esac
75 if test -f core ; then
76 ls -l core
77 exitcode=1
80 #(test $exitcode -ne 0) &&
81 { echo "Output from $M4:"; cat $tmpfile; }
83 exit $exitcode