Yuxiang Cao wrote: > I use ulimit -s to find stack size, which is 8192kbytes. Then I use > valgrind to record the stack size which give me this information. > test.sh: xmalloc: ../bash/unwind_prot.c:308: cannot allocate 172 bytes > (8359936 bytes allocated) > So from the above information I think this is not a stack overflow, > and that is a real fault in this program.
I disagree. Mostly because if you change the stack size ulimit then the example program will recurse more or less. Therefore it is definitely a stack limit policy that is limiting the behavior of the example program and not a bash bug. $ ulimit -s 8192 $ ./deep-stack-trial | wc -l 5340 $ ulimit -s 4096 $ ulimit -s 4096 $ ./deep-stack-trial | wc -l 2668 Now a smaller stack size. Now if the example test code is run it will be stopped sooner. $ ulimit -s 16384 bash: ulimit: stack size: cannot modify limit: Operation not permitted Prevented by operating system policy. Use a slightly smaller size. $ ulimit -s 16000 $ ulimit -s 16000 $ ./deep-stack-trial | wc -l 10441 Now a larger stack size. Now if the example test code is run it will be stopped later. It is the operating system stack size policy limit that is stopping the program. If you have sufficient permission then you may increase this value even to "unlimited". $ su - # ulimit -s unlimited # ulimit -s unlimited I would not advise this however. Those limits are placed there for the reason of containing unreasonable programs from accidentally creating unreasonable situations. Or at least unexpected ones. Just the same however if this is a limit that you personally disagree with then it is a limit that you may change on your system. If you want you may change your system to allow an unlimited level of recursion. Then if your system has the memory resources for it your program will be able to run to completion. If your system resources truly become exhausted then of course the program will still fail to complete successfully. But it won't be artifically limited by the system policy. Bash here in this context is simply running within the operating system limits imposed by the policy of the system as reflected in the stack size limits. Bob