From 23e212a9d0b5cfdff991a40ca264f9d1284f0d5f Mon Sep 17 00:00:00 2001 From: Xavier Bouchoux Date: Sat, 15 Oct 2022 16:58:29 +0200 Subject: [PATCH] std: check for overflow in dumpStackTraceFromBase same change as [68e26a2ceea85a1] "std: check for overflow in writeCurrentStackTrace" On arm64 macOS, the address of the last frame is 0x0 rather than a positive value like 0x1 on x86_64 macOS, therefore, we overflow an integer trying to subtract 1 when printing the stack trace. This patch fixes it by first checking for this condition before trying to subtract 1. Same behaviour on i386-windows-msvc. Note that we do not need to signal the `SignalIterator` about this as it will correctly detect this condition on the subsequent iteration and return `null`, thus terminating the loop. --- lib/std/debug.zig | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/std/debug.zig b/lib/std/debug.zig index cfe646647b..245e164489 100644 --- a/lib/std/debug.zig +++ b/lib/std/debug.zig @@ -187,7 +187,13 @@ pub fn dumpStackTraceFromBase(bp: usize, ip: usize) void { printSourceAtAddress(debug_info, stderr, ip, tty_config) catch return; var it = StackIterator.init(null, bp); while (it.next()) |return_address| { - printSourceAtAddress(debug_info, stderr, return_address - 1, tty_config) catch return; + // On arm64 macOS, the address of the last frame is 0x0 rather than 0x1 as on x86_64 macOS, + // therefore, we do a check for `return_address == 0` before subtracting 1 from it to avoid + // an overflow. We do not need to signal `StackIterator` as it will correctly detect this + // condition on the subsequent iteration and return `null` thus terminating the loop. + // same behaviour for i386-windows-msvc + const address = if (return_address == 0) return_address else return_address - 1; + printSourceAtAddress(debug_info, stderr, address, tty_config) catch return; } } } @@ -563,6 +569,7 @@ pub fn writeCurrentStackTrace( // therefore, we do a check for `return_address == 0` before subtracting 1 from it to avoid // an overflow. We do not need to signal `StackIterator` as it will correctly detect this // condition on the subsequent iteration and return `null` thus terminating the loop. + // same behaviour for i386-windows-msvc const address = if (return_address == 0) return_address else return_address - 1; try printSourceAtAddress(debug_info, out_stream, address, tty_config); }