authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-08-02 11:10:07+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-08-02 13:40:53-04:00
log68e26a2ceea85a149cb23286504cbdcec1ae814e
tree7d7baf8c489b53b21df872efd3c10463b29ab65e
parent871f6343f4d36c5c048f3307e2a38d184e248826

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. 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, 6 insertions(+), 1 deletions(-)

lib/std/debug.zig+6-1
......@@ -438,7 +438,12 @@ pub fn writeCurrentStackTrace(
438438 }
439439 var it = StackIterator.init(start_addr, null);
440440 while (it.next()) |return_address| {
441 try printSourceAtAddress(debug_info, out_stream, return_address - 1, tty_config);
441 // On arm64 macOS, the address of the last frame is 0x0 rather than 0x1 as on x86_64 macOS,
442 // therefore, we do a check for `return_address == 0` before subtracting 1 from it to avoid
443 // an overflow. We do not need to signal `StackIterator` as it will correctly detect this
444 // condition on the subsequent iteration and return `null` thus terminating the loop.
445 const address = if (return_address == 0) return_address else return_address - 1;
446 try printSourceAtAddress(debug_info, out_stream, address, tty_config);
442447 }
443448}
444449