File tree Expand file tree Collapse file tree 2 files changed +59
-1
lines changed Expand file tree Collapse file tree 2 files changed +59
-1
lines changed Original file line number Diff line number Diff line change 1- PROGS =proc-addr-space
1+ PROGS =proc-addr-space stack-overflow
22
33all : $(PROGS )
44
55proc-addr-space : proc-addr-space.o
66 $(CC ) -o $@ $^
77
8+ stack-overflow : stack-overflow.o
9+ $(CC ) -o $@ $^
10+
811clean :
912 rm -f * .o a.out $(PROGS )
1013
Original file line number Diff line number Diff line change 1+ /*
2+ * Demonstrate the behavior of process stack size limit.
3+ *
4+ * Compile with:
5+ * cc stack-overflow.c
6+ *
7+ * The first argument is the max recursion depth.
8+ *
9+ * Run with e.g.:
10+ * ulimit -s
11+ * ./a.out 8000
12+ *
13+ 14+ */
15+
16+ #define _XOPEN_SOURCE 700
17+
18+ #include <stdint.h>
19+ #include <stdio.h>
20+ #include <string.h>
21+ #include <unistd.h>
22+ #include <stdlib.h>
23+ #include <err.h>
24+ #include <signal.h>
25+
26+ void
27+ recurse (size_t n )
28+ {
29+ /* Intentionally unused variable to make the stack bigger */
30+ char foo [1024 ];
31+
32+ /* To silence the compiler warning */
33+ (void ) foo ;
34+ printf ("%zu " , n );
35+ fflush (stdout );
36+
37+ if (n == 0 ) {
38+ printf ("\n" );
39+ return ;
40+ }
41+
42+ recurse (-- n );
43+ }
44+
45+ int
46+ main (int argc , char * argv [])
47+ {
48+ if (argc != 2 )
49+ errx (1 , "Usage: %s <iterations>" , argv [0 ]);
50+
51+ /* Should have used strtoull(). */
52+ recurse (atoi (argv [1 ]));
53+
54+ return (0 );
55+ }
You can’t perform that action at this time.
0 commit comments