aboutsummaryrefslogtreecommitdiff
path: root/lib/std/debug.zig
diff options
context:
space:
mode:
authorJakub Konka <kubkon@jakubkonka.com>2021-08-02 11:10:07 +0200
committerAndrew Kelley <andrew@ziglang.org>2021-08-02 13:40:53 -0400
commit68e26a2ceea85a149cb23286504cbdcec1ae814e (patch)
tree7d7baf8c489b53b21df872efd3c10463b29ab65e /lib/std/debug.zig
parent871f6343f4d36c5c048f3307e2a38d184e248826 (diff)
downloadzig-68e26a2ceea85a149cb23286504cbdcec1ae814e.tar.gz
zig-68e26a2ceea85a149cb23286504cbdcec1ae814e.zip
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.
Diffstat (limited to 'lib/std/debug.zig')
-rw-r--r--lib/std/debug.zig7
1 files changed, 6 insertions, 1 deletions
diff --git a/lib/std/debug.zig b/lib/std/debug.zig
index f74d0b3f91..c3ef9dec4c 100644
--- a/lib/std/debug.zig
+++ b/lib/std/debug.zig
@@ -438,7 +438,12 @@ pub fn writeCurrentStackTrace(
}
var it = StackIterator.init(start_addr, null);
while (it.next()) |return_address| {
- try printSourceAtAddress(debug_info, out_stream, return_address - 1, tty_config);
+ // 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.
+ const address = if (return_address == 0) return_address else return_address - 1;
+ try printSourceAtAddress(debug_info, out_stream, address, tty_config);
}
}