authorgravatar for 14938807+xackus@users.noreply.github.comxackus <14938807+xackus@users.noreply.github.com> 2020-05-11 14:06:37+02:00
committergravatar for 14938807+xackus@users.noreply.github.comxackus <14938807+xackus@users.noreply.github.com> 2020-05-11 14:06:37+02:00
log204f8daeeda02738479e0d586e6426e23f78e6cc
treecd38de48598d099b4fa8596f003cd536494671d9
parentd4d509090b47a3860831a0fb6fb7fae02f5f7857

stage1: detect underflow in bigint_fits_in_bits


2 files changed, 9 insertions(+), 0 deletions(-)

src/bigint.cpp+1
...@@ -243,6 +243,7 @@ bool bigint_fits_in_bits(const BigInt *bn, size_t bit_count, bool is_signed) {...@@ -243,6 +243,7 @@ bool bigint_fits_in_bits(const BigInt *bn, size_t bit_count, bool is_signed) {
243 }243 }
244244
245 if (!is_signed) {245 if (!is_signed) {
246 if(bn->is_negative) return false;
246 size_t full_bits = bn->digit_count * 64;247 size_t full_bits = bn->digit_count * 64;
247 size_t leading_zero_count = bigint_clz(bn, full_bits);248 size_t leading_zero_count = bigint_clz(bn, full_bits);
248 return bit_count >= full_bits - leading_zero_count;249 return bit_count >= full_bits - leading_zero_count;
test/compile_errors.zig+8
...@@ -7411,4 +7411,12 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -7411,4 +7411,12 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
7411 , &[_][]const u8{7411 , &[_][]const u8{
7412 ":5:28: error: expected type '[]u8', found '*const [3:0]u8'",7412 ":5:28: error: expected type '[]u8', found '*const [3:0]u8'",
7413 });7413 });
7414
7415 cases.add("integer underflow error",
7416 \\export fn entry() void {
7417 \\ _ = @intToPtr(*c_void, ~@as(usize, @import("std").math.maxInt(usize)) - 1);
7418 \\}
7419 , &[_][]const u8{
7420 ":2:75: error: operation caused overflow",
7421 });
7414}7422}