-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Conversation
not sure if I should add test for this |
Probably should add a test yes :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure if I should add test for this
The default answer to this question is always yes.
src/Sema.zig
Outdated
.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), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.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.
There was a problem hiding this comment.
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;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was trying to avoid the extra call to getOrPutStringFmt
in the .anon_struct_type
case, but the new code is certainly reasonable enough that I won't hold up the merge over it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use Type.structFieldName
and also update the equivalent code in coerceTupleToTuple
to use it.
I promise I'll merge this as soon as it passes the CI.
Done. However I'm not sure if pr won't reverted because of this #18071 (comment) |
Right, guess I'll leave it then. |
I'll be waiting. No matter how long it takes |
Co-authored-by: Veikka Tuominen <git@vexu.eu>
Co-authored-by: Veikka Tuominen <git@vexu.eu>
Closes #19529