Skip to content

Commit 09f2731

Browse files
committed
stage2: check for zero in @intToError safety
1 parent 7c9979a commit 09f2731

File tree

4 files changed

+34
-11
lines changed

4 files changed

+34
-11
lines changed

src/Sema.zig

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6804,11 +6804,10 @@ fn zirErrorToInt(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
68046804
const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
68056805
const uncasted_operand = try sema.resolveInst(extra.operand);
68066806
const operand = try sema.coerce(block, Type.anyerror, uncasted_operand, operand_src);
6807-
const result_ty = Type.u16;
68086807

68096808
if (try sema.resolveMaybeUndefVal(block, src, operand)) |val| {
68106809
if (val.isUndef()) {
6811-
return sema.addConstUndef(result_ty);
6810+
return sema.addConstUndef(Type.err_int);
68126811
}
68136812
switch (val.tag()) {
68146813
.@"error" => {
@@ -6817,14 +6816,14 @@ fn zirErrorToInt(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
68176816
.base = .{ .tag = .int_u64 },
68186817
.data = (try sema.mod.getErrorValue(val.castTag(.@"error").?.data.name)).value,
68196818
};
6820-
return sema.addConstant(result_ty, Value.initPayload(&payload.base));
6819+
return sema.addConstant(Type.err_int, Value.initPayload(&payload.base));
68216820
},
68226821

68236822
// This is not a valid combination with the type `anyerror`.
68246823
.the_only_possible_value => unreachable,
68256824

68266825
// Assume it's already encoded as an integer.
6827-
else => return sema.addConstant(result_ty, val),
6826+
else => return sema.addConstant(Type.err_int, val),
68286827
}
68296828
}
68306829

@@ -6833,14 +6832,14 @@ fn zirErrorToInt(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
68336832
if (!op_ty.isAnyError()) {
68346833
const names = op_ty.errorSetNames();
68356834
switch (names.len) {
6836-
0 => return sema.addConstant(result_ty, Value.zero),
6837-
1 => return sema.addIntUnsigned(result_ty, sema.mod.global_error_set.get(names[0]).?),
6835+
0 => return sema.addConstant(Type.err_int, Value.zero),
6836+
1 => return sema.addIntUnsigned(Type.err_int, sema.mod.global_error_set.get(names[0]).?),
68386837
else => {},
68396838
}
68406839
}
68416840

68426841
try sema.requireRuntimeBlock(block, src, operand_src);
6843-
return block.addBitCast(result_ty, operand);
6842+
return block.addBitCast(Type.err_int, operand);
68446843
}
68456844

68466845
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
68516850
const src = LazySrcLoc.nodeOffset(extra.node);
68526851
const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = extra.node };
68536852
const uncasted_operand = try sema.resolveInst(extra.operand);
6854-
const operand = try sema.coerce(block, Type.u16, uncasted_operand, operand_src);
6853+
const operand = try sema.coerce(block, Type.err_int, uncasted_operand, operand_src);
68556854
const target = sema.mod.getTarget();
68566855

68576856
if (try sema.resolveDefinedValue(block, operand_src, operand)) |value| {
@@ -6868,7 +6867,10 @@ fn zirIntToError(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
68686867
try sema.requireRuntimeBlock(block, src, operand_src);
68696868
if (block.wantSafety()) {
68706869
const is_lt_len = try block.addUnOp(.cmp_lt_errors_len, operand);
6871-
try sema.addSafetyCheck(block, is_lt_len, .invalid_error_code);
6870+
const zero_val = try sema.addConstant(Type.err_int, Value.zero);
6871+
const is_non_zero = try block.addBinOp(.cmp_neq, operand, zero_val);
6872+
const ok = try block.addBinOp(.bit_and, is_lt_len, is_non_zero);
6873+
try sema.addSafetyCheck(block, ok, .invalid_error_code);
68726874
}
68736875
return block.addInst(.{
68746876
.tag = .bitcast,
@@ -17360,7 +17362,7 @@ fn zirErrSetCast(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
1736017362

1736117363
try sema.requireRuntimeBlock(block, src, operand_src);
1736217364
if (block.wantSafety() and !dest_ty.isAnyError() and sema.mod.comp.bin_file.options.use_llvm) {
17363-
const err_int_inst = try block.addBitCast(Type.u16, operand);
17365+
const err_int_inst = try block.addBitCast(Type.err_int, operand);
1736417366
const ok = try block.addTyOp(.error_set_has_value, dest_ty, err_int_inst);
1736517367
try sema.addSafetyCheck(block, ok, .invalid_error_code);
1736617368
}

src/codegen/llvm.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8005,7 +8005,7 @@ pub const FuncGen = struct {
80058005
.data = err_int,
80068006
};
80078007
break :int try self.dg.lowerValue(.{
8008-
.ty = Type.u16,
8008+
.ty = Type.err_int,
80098009
.val = Value.initPayload(&tag_val_payload.base),
80108010
});
80118011
};

src/type.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6303,6 +6303,8 @@ pub const Type = extern union {
63036303
pub const @"anyopaque" = initTag(.anyopaque);
63046304
pub const @"null" = initTag(.@"null");
63056305

6306+
pub const err_int = Type.u16;
6307+
63066308
pub fn ptr(arena: Allocator, mod: *Module, data: Payload.Pointer.Data) !Type {
63076309
const target = mod.getTarget();
63086310

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const std = @import("std");
2+
3+
pub fn panic(message: []const u8, stack_trace: ?*std.builtin.StackTrace) noreturn {
4+
_ = stack_trace;
5+
if (std.mem.eql(u8, message, "invalid error code")) {
6+
std.process.exit(0);
7+
}
8+
std.process.exit(1);
9+
}
10+
pub fn main() !void {
11+
bar(0) catch {};
12+
return error.TestFailed;
13+
}
14+
fn bar(x: u16) anyerror {
15+
return @intToError(x);
16+
}
17+
// run
18+
// backend=llvm
19+
// target=native

0 commit comments

Comments
 (0)