@@ -13,6 +13,9 @@ const native_arch = builtin.cpu.arch;
13
13
const native_os = builtin .os .tag ;
14
14
const native_endian = native_arch .endian ();
15
15
16
+ /// Maximum distance to walk when iterating through a stack trace.
17
+ pub const max_stack_trace_depth = 50 ;
18
+
16
19
pub const MemoryAccessor = @import ("debug/MemoryAccessor.zig" );
17
20
pub const FixedBufferReader = @import ("debug/FixedBufferReader.zig" );
18
21
pub const Dwarf = @import ("debug/Dwarf.zig" );
@@ -458,6 +461,8 @@ pub fn dumpStackTraceFromBase(context: *ThreadContext) void {
458
461
printSourceAtAddress (debug_info , stderr , pc_addr , tty_config ) catch return ;
459
462
}
460
463
464
+ var depth : usize = 0 ;
465
+
461
466
while (it .next ()) | return_address | {
462
467
printLastUnwindError (& it , debug_info , stderr , tty_config );
463
468
@@ -468,6 +473,12 @@ pub fn dumpStackTraceFromBase(context: *ThreadContext) void {
468
473
// same behaviour for x86-windows-msvc
469
474
const address = if (return_address == 0 ) return_address else return_address - 1 ;
470
475
printSourceAtAddress (debug_info , stderr , address , tty_config ) catch return ;
476
+
477
+ depth += 1 ;
478
+ if (depth > max_stack_trace_depth ) {
479
+ stderr .print ("Abandoned stack trace after {} frames.\n " , .{max_stack_trace_depth }) catch {};
480
+ break ;
481
+ }
471
482
} else printLastUnwindError (& it , debug_info , stderr , tty_config );
472
483
}
473
484
}
@@ -870,8 +881,14 @@ pub const StackIterator = struct {
870
881
var address = it .next_internal () orelse return null ;
871
882
872
883
if (it .first_address ) | first_address | {
884
+ var depth : usize = 0 ;
873
885
while (address != first_address ) {
874
886
address = it .next_internal () orelse return null ;
887
+
888
+ depth += 1 ;
889
+ if (depth > max_stack_trace_depth ) {
890
+ return null ;
891
+ }
875
892
}
876
893
it .first_address = null ;
877
894
}
@@ -980,6 +997,8 @@ pub fn writeCurrentStackTrace(
980
997
} else null ) orelse StackIterator .init (start_addr , null );
981
998
defer it .deinit ();
982
999
1000
+ var depth : usize = 0 ;
1001
+
983
1002
while (it .next ()) | return_address | {
984
1003
printLastUnwindError (& it , debug_info , out_stream , tty_config );
985
1004
@@ -990,6 +1009,12 @@ pub fn writeCurrentStackTrace(
990
1009
// same behaviour for x86-windows-msvc
991
1010
const address = return_address - | 1 ;
992
1011
try printSourceAtAddress (debug_info , out_stream , address , tty_config );
1012
+
1013
+ depth += 1 ;
1014
+ if (depth > max_stack_trace_depth ) {
1015
+ out_stream .print ("Abandoned stack trace after {} frames.\n " , .{max_stack_trace_depth }) catch {};
1016
+ break ;
1017
+ }
993
1018
} else printLastUnwindError (& it , debug_info , out_stream , tty_config );
994
1019
}
995
1020
0 commit comments