Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sema: fix OOB access in coerceTupleToStruct #19620

Merged
merged 15 commits into from
Jul 21, 2024
Merged
15 changes: 8 additions & 7 deletions src/Sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -31440,13 +31440,14 @@ fn coerceTupleToStruct(
};
for (0..field_count) |tuple_field_index| {
const field_src = inst_src; // TODO better source location
const field_name: InternPool.NullTerminatedString = switch (ip.indexToKey(inst_ty.toIntern())) {
.anon_struct_type => |anon_struct_type| if (anon_struct_type.names.len > 0)
anon_struct_type.names.get(ip)[tuple_field_index]
else
try ip.getOrPutStringFmt(sema.gpa, "{d}", .{tuple_field_index}, .no_embedded_nulls),
.struct_type => ip.loadStructType(inst_ty.toIntern()).field_names.get(ip)[tuple_field_index],
else => unreachable,
const field_name: InternPool.NullTerminatedString = blk: {
const field_name_optional = switch (ip.indexToKey(inst_ty.toIntern())) {
.anon_struct_type => |anon_struct_type| anon_struct_type.fieldName(ip, tuple_field_index),
.struct_type => ip.loadStructType(inst_ty.toIntern()).fieldName(ip, tuple_field_index),
else => unreachable,
};
break :blk field_name_optional.unwrap() orelse
try ip.getOrPutStringFmt(sema.gpa, "{d}", .{tuple_field_index}, .no_embedded_nulls);
};
const struct_field_index = try sema.structFieldIndex(block, struct_ty, field_name, field_src);
const struct_field_ty = Type.fromInterned(struct_type.field_types.get(ip)[struct_field_index]);
Expand Down
13 changes: 13 additions & 0 deletions test/cases/compile_errors/invalid_tuple_to_struct_coercion.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const S = struct {
fizz: void,
};

export fn entry() void {
_ = @as(S, struct { void }{{}});
}

// error
// target=native
//
// :6:31: error: no field named '0' in struct 'tmp.S'
// :1:11: note: struct declared here
Loading