From 987768778a67538299f84a6ab7ff0ca65f69d2ac Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sat, 19 Aug 2017 01:32:15 -0400 Subject: bit shifting safety * add u3, u4, u5, u6, u7 and i3, i4, i5, i6, i7 * shift operations shift amount parameter type is integer with log2 bit width of other param - This enforces not violating undefined behavior on shift amount >= bit width with the type system * clean up math.log, math.ln, math.log2, math.log10 closes #403 --- std/debug.zig | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'std/debug.zig') diff --git a/std/debug.zig b/std/debug.zig index ab14f33eee..7652807ca3 100644 --- a/std/debug.zig +++ b/std/debug.zig @@ -1,3 +1,4 @@ +const math = @import("math/index.zig"); const mem = @import("mem.zig"); const io = @import("io.zig"); const os = @import("os/index.zig"); @@ -893,13 +894,14 @@ fn readInitialLength(in_stream: &io.InStream, is_64: &bool) -> %u64 { fn readULeb128(in_stream: &io.InStream) -> %u64 { var result: u64 = 0; - var shift: u64 = 0; + var shift: usize = 0; while (true) { const byte = %return in_stream.readByte(); + var operand: u64 = undefined; - if (@shlWithOverflow(u64, byte & 0b01111111, shift, &operand)) + if (@shlWithOverflow(u64, byte & 0b01111111, u6(shift), &operand)) return error.InvalidDebugInfo; result |= operand; @@ -913,13 +915,14 @@ fn readULeb128(in_stream: &io.InStream) -> %u64 { fn readILeb128(in_stream: &io.InStream) -> %i64 { var result: i64 = 0; - var shift: i64 = 0; + var shift: usize = 0; while (true) { const byte = %return in_stream.readByte(); + var operand: i64 = undefined; - if (@shlWithOverflow(i64, byte & 0b01111111, shift, &operand)) + if (@shlWithOverflow(i64, byte & 0b01111111, u6(shift), &operand)) return error.InvalidDebugInfo; result |= operand; @@ -927,8 +930,7 @@ fn readILeb128(in_stream: &io.InStream) -> %i64 { if ((byte & 0b10000000) == 0) { if (shift < @sizeOf(i64) * 8 and (byte & 0b01000000) != 0) - result |= -(i64(1) << shift); - + result |= -(i64(1) << u6(shift)); return result; } } -- cgit v1.2.3