authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-05-12 01:25:24+03:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-05-12 01:25:24+03:00
log7aee8a93ebc199dd5ff94387f0906699d795c138
tree2a500c59212998b0dfb0671cf8f8556f9de0f558
parent3e3c651b670fb45e714db1cfb32428c3ea3cd828
parent204f8daeeda02738479e0d586e6426e23f78e6cc
signature Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #5315 from xackus/fix-bigint_fits_in_bits

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}