authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-01-01 15:24:46+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-01-01 13:11:42-05:00
log1b64a5f5f0cece3cf0009410ddb13b5dd6f899e1
treeaf85941d89afe30f1a184b39f858ca12c12e1280
parent0fb3e6608c160a0cd9db75fe1ba3d5bb7dac8fa8

fix segfault in bit shift safety check


2 files changed, 11 insertions(+), 3 deletions(-)

src/ir.cpp+7-3
...@@ -15621,11 +15621,15 @@ static IrInstruction *ir_analyze_bit_shift(IrAnalyze *ira, IrInstructionBinOp *b...@@ -15621,11 +15621,15 @@ static IrInstruction *ir_analyze_bit_shift(IrAnalyze *ira, IrInstructionBinOp *b
15621 op1->value->type->data.integral.bit_count - 1);15621 op1->value->type->data.integral.bit_count - 1);
15622 if (bin_op_instruction->op_id == IrBinOpBitShiftLeftLossy &&15622 if (bin_op_instruction->op_id == IrBinOpBitShiftLeftLossy &&
15623 op2->value->type->id == ZigTypeIdComptimeInt) {15623 op2->value->type->id == ZigTypeIdComptimeInt) {
15624 if (!bigint_fits_in_bits(&op2->value->data.x_bigint,15624
15625 ZigValue *op2_val = ir_resolve_const(ira, op2, UndefBad);
15626 if (op2_val == nullptr)
15627 return ira->codegen->invalid_instruction;
15628 if (!bigint_fits_in_bits(&op2_val->data.x_bigint,
15625 shift_amt_type->data.integral.bit_count,15629 shift_amt_type->data.integral.bit_count,
15626 op2->value->data.x_bigint.is_negative)) {15630 op2_val->data.x_bigint.is_negative)) {
15627 Buf *val_buf = buf_alloc();15631 Buf *val_buf = buf_alloc();
15628 bigint_append_buf(val_buf, &op2->value->data.x_bigint, 10);15632 bigint_append_buf(val_buf, &op2_val->data.x_bigint, 10);
15629 ErrorMsg* msg = ir_add_error(ira,15633 ErrorMsg* msg = ir_add_error(ira,
15630 &bin_op_instruction->base,15634 &bin_op_instruction->base,
15631 buf_sprintf("RHS of shift is too large for LHS type"));15635 buf_sprintf("RHS of shift is too large for LHS type"));
test/stage1/behavior/bit_shifting.zig+4
...@@ -96,3 +96,7 @@ test "comptime shr of BigInt" {...@@ -96,3 +96,7 @@ test "comptime shr of BigInt" {
96 std.debug.assert(n1 >> 64 == 0);96 std.debug.assert(n1 >> 64 == 0);
97 }97 }
98}98}
99
100test "comptime shift safety check" {
101 const x = @as(usize, 42) << @sizeOf(usize);
102}