mirror of
https://github.com/ziglang/zig.git
synced 2026-02-14 13:30:45 +00:00
stage2: check for zero in @intToError safety
This commit is contained in:
parent
7c9979a02e
commit
09f273136c
22
src/Sema.zig
22
src/Sema.zig
@ -6804,11 +6804,10 @@ fn zirErrorToInt(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
|
||||
const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
|
||||
const uncasted_operand = try sema.resolveInst(extra.operand);
|
||||
const operand = try sema.coerce(block, Type.anyerror, uncasted_operand, operand_src);
|
||||
const result_ty = Type.u16;
|
||||
|
||||
if (try sema.resolveMaybeUndefVal(block, src, operand)) |val| {
|
||||
if (val.isUndef()) {
|
||||
return sema.addConstUndef(result_ty);
|
||||
return sema.addConstUndef(Type.err_int);
|
||||
}
|
||||
switch (val.tag()) {
|
||||
.@"error" => {
|
||||
@ -6817,14 +6816,14 @@ fn zirErrorToInt(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
|
||||
.base = .{ .tag = .int_u64 },
|
||||
.data = (try sema.mod.getErrorValue(val.castTag(.@"error").?.data.name)).value,
|
||||
};
|
||||
return sema.addConstant(result_ty, Value.initPayload(&payload.base));
|
||||
return sema.addConstant(Type.err_int, Value.initPayload(&payload.base));
|
||||
},
|
||||
|
||||
// This is not a valid combination with the type `anyerror`.
|
||||
.the_only_possible_value => unreachable,
|
||||
|
||||
// Assume it's already encoded as an integer.
|
||||
else => return sema.addConstant(result_ty, val),
|
||||
else => return sema.addConstant(Type.err_int, val),
|
||||
}
|
||||
}
|
||||
|
||||
@ -6833,14 +6832,14 @@ fn zirErrorToInt(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
|
||||
if (!op_ty.isAnyError()) {
|
||||
const names = op_ty.errorSetNames();
|
||||
switch (names.len) {
|
||||
0 => return sema.addConstant(result_ty, Value.zero),
|
||||
1 => return sema.addIntUnsigned(result_ty, sema.mod.global_error_set.get(names[0]).?),
|
||||
0 => return sema.addConstant(Type.err_int, Value.zero),
|
||||
1 => return sema.addIntUnsigned(Type.err_int, sema.mod.global_error_set.get(names[0]).?),
|
||||
else => {},
|
||||
}
|
||||
}
|
||||
|
||||
try sema.requireRuntimeBlock(block, src, operand_src);
|
||||
return block.addBitCast(result_ty, operand);
|
||||
return block.addBitCast(Type.err_int, operand);
|
||||
}
|
||||
|
||||
fn zirIntToError(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref {
|
||||
@ -6851,7 +6850,7 @@ fn zirIntToError(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
|
||||
const src = LazySrcLoc.nodeOffset(extra.node);
|
||||
const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
|
||||
const uncasted_operand = try sema.resolveInst(extra.operand);
|
||||
const operand = try sema.coerce(block, Type.u16, uncasted_operand, operand_src);
|
||||
const operand = try sema.coerce(block, Type.err_int, uncasted_operand, operand_src);
|
||||
const target = sema.mod.getTarget();
|
||||
|
||||
if (try sema.resolveDefinedValue(block, operand_src, operand)) |value| {
|
||||
@ -6868,7 +6867,10 @@ fn zirIntToError(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
|
||||
try sema.requireRuntimeBlock(block, src, operand_src);
|
||||
if (block.wantSafety()) {
|
||||
const is_lt_len = try block.addUnOp(.cmp_lt_errors_len, operand);
|
||||
try sema.addSafetyCheck(block, is_lt_len, .invalid_error_code);
|
||||
const zero_val = try sema.addConstant(Type.err_int, Value.zero);
|
||||
const is_non_zero = try block.addBinOp(.cmp_neq, operand, zero_val);
|
||||
const ok = try block.addBinOp(.bit_and, is_lt_len, is_non_zero);
|
||||
try sema.addSafetyCheck(block, ok, .invalid_error_code);
|
||||
}
|
||||
return block.addInst(.{
|
||||
.tag = .bitcast,
|
||||
@ -17360,7 +17362,7 @@ fn zirErrSetCast(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
|
||||
|
||||
try sema.requireRuntimeBlock(block, src, operand_src);
|
||||
if (block.wantSafety() and !dest_ty.isAnyError() and sema.mod.comp.bin_file.options.use_llvm) {
|
||||
const err_int_inst = try block.addBitCast(Type.u16, operand);
|
||||
const err_int_inst = try block.addBitCast(Type.err_int, operand);
|
||||
const ok = try block.addTyOp(.error_set_has_value, dest_ty, err_int_inst);
|
||||
try sema.addSafetyCheck(block, ok, .invalid_error_code);
|
||||
}
|
||||
|
||||
@ -8005,7 +8005,7 @@ pub const FuncGen = struct {
|
||||
.data = err_int,
|
||||
};
|
||||
break :int try self.dg.lowerValue(.{
|
||||
.ty = Type.u16,
|
||||
.ty = Type.err_int,
|
||||
.val = Value.initPayload(&tag_val_payload.base),
|
||||
});
|
||||
};
|
||||
|
||||
@ -6303,6 +6303,8 @@ pub const Type = extern union {
|
||||
pub const @"anyopaque" = initTag(.anyopaque);
|
||||
pub const @"null" = initTag(.@"null");
|
||||
|
||||
pub const err_int = Type.u16;
|
||||
|
||||
pub fn ptr(arena: Allocator, mod: *Module, data: Payload.Pointer.Data) !Type {
|
||||
const target = mod.getTarget();
|
||||
|
||||
|
||||
19
test/cases/safety/zero casted to error.zig
Normal file
19
test/cases/safety/zero casted to error.zig
Normal file
@ -0,0 +1,19 @@
|
||||
const std = @import("std");
|
||||
|
||||
pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
|
||||
_ = stack_trace;
|
||||
if (std.mem.eql(u8, message, "invalid error code")) {
|
||||
std.process.exit(0);
|
||||
}
|
||||
std.process.exit(1);
|
||||
}
|
||||
pub fn main() !void {
|
||||
bar(0) catch {};
|
||||
return error.TestFailed;
|
||||
}
|
||||
fn bar(x: u16) anyerror {
|
||||
return @intToError(x);
|
||||
}
|
||||
// run
|
||||
// backend=llvm
|
||||
// target=native
|
||||
Loading…
x
Reference in New Issue
Block a user