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
5 changes: 4 additions & 1 deletion src/Sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -32334,7 +32334,10 @@ fn coerceTupleToStruct(
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],
.struct_type => if (ip.loadStructType(inst_ty.toIntern()).field_names.len > 0)
ip.loadStructType(inst_ty.toIntern()).field_names.get(ip)[tuple_field_index]
else
try ip.getOrPutStringFmt(sema.gpa, "{d}", .{tuple_field_index}, .no_embedded_nulls),
Copy link
Member

@jacobly0 jacobly0 Apr 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.struct_type => if (ip.loadStructType(inst_ty.toIntern()).field_names.len > 0)
ip.loadStructType(inst_ty.toIntern()).field_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()).fieldName(ip, tuple_field_index).unwrap() orelse
try ip.getOrPutStringFmt(sema.gpa, "{d}", .{tuple_field_index}, .no_embedded_nulls),

Note that the same thing can be done in the .anon_struct_type case.

Copy link
Member

@jacobly0 jacobly0 Apr 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although, really it should probably be more like:

        const maybe_field_name = 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,
        };
        const struct_field_index = if (maybe_field_name.unwrap()) |field_name|
            try sema.structFieldIndex(block, struct_ty, field_name, field_src)
        else
            tuple_field_index;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

else => unreachable,
};
const struct_field_index = try sema.structFieldIndex(block, struct_ty, field_name, field_src);
Expand Down