home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / m4-1.4-src.tgz / tar.out / fsf / m4 / examples / stackovf.sh < prev    next >
Linux/UNIX/POSIX Shell Script  |  1996-09-28  |  2KB  |  84 lines

  1. #!/bin/sh
  2.  
  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)
  6.  
  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
  16.     fi
  17.     done
  18. }
  19.  
  20. PATH=.:..:$PATH; export PATH;
  21. M4=m4
  22. type $M4
  23.  
  24. tmpfile=/tmp/t.$$
  25. trap 'rm -f $tmpfile; exit 1' 1 2 3 15
  26.  
  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";
  33. }
  34. for ($i=$max-1; $i>=0; $i--) {
  35.     print "body with substance no. $i)dnl\n"
  36. }
  37. ' | \
  38. (
  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..."
  52. fi
  53. $M4 -L999999999 > $tmpfile 2>&1
  54. )
  55. result=$?
  56.  
  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
  66.     ;;
  67.     *ut*of*emory*)
  68.         echo "*** Test is INCONCLUSIVE (ran out of heap before stack overflow)";
  69.     ;;
  70.     *)    echo "*** Test FAILED.  $M4 aborted unexpectedly.  Output:";
  71.     ;;
  72.     esac
  73. fi
  74.  
  75. if test -f core ; then
  76.     ls -l core
  77.     exitcode=1
  78. fi
  79.  
  80. #(test $exitcode -ne 0) &&
  81.     { echo "Output from $M4:"; cat $tmpfile; }
  82.  
  83. exit $exitcode
  84.