authorgravatar for xavierb@gmail.comXavier Bouchoux <xavierb@gmail.com> 2022-10-15 16:58:29+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-10-18 13:03:22+02:00
log23e212a9d0b5cfdff991a40ca264f9d1284f0d5f
treee53a70f813fb78130c7b5dfc08cef27f3b794aaf
parentbd0dd225e843801b4b6162cbd2709c7325ab87b7

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.

1 files changed, 8 insertions(+), 1 deletions(-)

lib/std/debug.zig+8-1
...@@ -187,7 +187,13 @@ pub fn dumpStackTraceFromBase(bp: usize, ip: usize) void {...@@ -187,7 +187,13 @@ pub fn dumpStackTraceFromBase(bp: usize, ip: usize) void {
187 printSourceAtAddress(debug_info, stderr, ip, tty_config) catch return;187 printSourceAtAddress(debug_info, stderr, ip, tty_config) catch return;
188 var it = StackIterator.init(null, bp);188 var it = StackIterator.init(null, bp);
189 while (it.next()) |return_address| {189 while (it.next()) |return_address| {
190 printSourceAtAddress(debug_info, stderr, return_address - 1, tty_config) catch return;190 // On arm64 macOS, the address of the last frame is 0x0 rather than 0x1 as on x86_64 macOS,
191 // therefore, we do a check for `return_address == 0` before subtracting 1 from it to avoid
192 // an overflow. We do not need to signal `StackIterator` as it will correctly detect this
193 // condition on the subsequent iteration and return `null` thus terminating the loop.
194 // same behaviour for i386-windows-msvc
195 const address = if (return_address == 0) return_address else return_address - 1;
196 printSourceAtAddress(debug_info, stderr, address, tty_config) catch return;
191 }197 }
192 }198 }
193}199}
...@@ -563,6 +569,7 @@ pub fn writeCurrentStackTrace(...@@ -563,6 +569,7 @@ pub fn writeCurrentStackTrace(
563 // therefore, we do a check for `return_address == 0` before subtracting 1 from it to avoid569 // therefore, we do a check for `return_address == 0` before subtracting 1 from it to avoid
564 // an overflow. We do not need to signal `StackIterator` as it will correctly detect this570 // an overflow. We do not need to signal `StackIterator` as it will correctly detect this
565 // condition on the subsequent iteration and return `null` thus terminating the loop.571 // condition on the subsequent iteration and return `null` thus terminating the loop.
572 // same behaviour for i386-windows-msvc
566 const address = if (return_address == 0) return_address else return_address - 1;573 const address = if (return_address == 0) return_address else return_address - 1;
567 try printSourceAtAddress(debug_info, out_stream, address, tty_config);574 try printSourceAtAddress(debug_info, out_stream, address, tty_config);
568 }575 }