aboutsummaryrefslogtreecommitdiff
path: root/src/codegen
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2023-11-01 06:33:40 -0400
committerGitHub <noreply@github.com>2023-11-01 06:33:40 -0400
commit725f765c376dda291d3d5afe5446221a97c189f7 (patch)
treed2c1d77794ba13140aa4a3f0303d56bbec2bd27f /src/codegen
parent46062f1c13d8ee9eebf49806660c2271f2acc613 (diff)
parent13b1e10b8f3d8df92417999ed972e5f682c82a46 (diff)
downloadzig-725f765c376dda291d3d5afe5446221a97c189f7.tar.gz
zig-725f765c376dda291d3d5afe5446221a97c189f7.zip
Merge pull request #17802 from jacobly0/read-write-int
mem: fix UB in `readInt`/`writeInt` and delete variants
Diffstat (limited to 'src/codegen')
-rw-r--r--src/codegen/c.zig21
-rw-r--r--src/codegen/llvm.zig6
2 files changed, 15 insertions, 12 deletions
diff --git a/src/codegen/c.zig b/src/codegen/c.zig
index 83d921ed9f..0178ffef9a 100644
--- a/src/codegen/c.zig
+++ b/src/codegen/c.zig
@@ -4652,7 +4652,10 @@ fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !Loca
try writer.writeAll(", &");
try f.writeCValue(writer, operand_lval, .Other);
try writer.writeAll(", sizeof(");
- try f.renderType(writer, dest_ty);
+ try f.renderType(
+ writer,
+ if (dest_ty.abiSize(mod) <= operand_ty.abiSize(mod)) dest_ty else operand_ty,
+ );
try writer.writeAll("));\n");
// Ensure padding bits have the expected value.
@@ -4666,8 +4669,8 @@ fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !Loca
try f.writeCValue(writer, local, .Other);
if (dest_cty.castTag(.array)) |pl| {
try writer.print("[{d}]", .{switch (target.cpu.arch.endian()) {
- .Little => pl.data.len - 1,
- .Big => 0,
+ .little => pl.data.len - 1,
+ .big => 0,
}});
const elem_cty = f.indexToCType(pl.data.elem_type);
wrap_cty = elem_cty.toSignedness(dest_info.signedness);
@@ -4697,8 +4700,8 @@ fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !Loca
try f.writeCValue(writer, local, .Other);
if (dest_cty.castTag(.array)) |pl| {
try writer.print("[{d}]", .{switch (target.cpu.arch.endian()) {
- .Little => pl.data.len - 1,
- .Big => 0,
+ .little => pl.data.len - 1,
+ .big => 0,
}});
}
if (need_bitcasts) try writer.writeByte(')');
@@ -7652,13 +7655,13 @@ fn formatIntLiteral(
else => .{
.cty = CType.initTag(.void),
.count = 1,
- .endian = .Little,
+ .endian = .little,
.homogeneous = true,
},
.zig_u128, .zig_i128 => .{
.cty = CType.initTag(.uint64_t),
.count = 2,
- .endian = .Big,
+ .endian = .big,
.homogeneous = false,
},
.array => info: {
@@ -7729,8 +7732,8 @@ fn formatIntLiteral(
const most_significant_limb_i = wrap.len - limbs_per_c_limb;
while (limb_offset < wrap.len) : (limb_offset += limbs_per_c_limb) {
const limb_i = switch (c_limb_info.endian) {
- .Little => limb_offset,
- .Big => most_significant_limb_i - limb_offset,
+ .little => limb_offset,
+ .big => most_significant_limb_i - limb_offset,
};
var c_limb_mut = BigInt.Mutable{
.limbs = wrap.limbs[limb_i..][0..limbs_per_c_limb],
diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig
index 9edc456b35..356e4c318b 100644
--- a/src/codegen/llvm.zig
+++ b/src/codegen/llvm.zig
@@ -342,8 +342,8 @@ const DataLayoutBuilder = struct {
writer: anytype,
) @TypeOf(writer).Error!void {
try writer.writeByte(switch (self.target.cpu.arch.endian()) {
- .Little => 'e',
- .Big => 'E',
+ .little => 'e',
+ .big => 'E',
});
switch (self.target.cpu.arch) {
.amdgcn,
@@ -10453,7 +10453,7 @@ pub const FuncGen = struct {
else
payload_llvm_ty;
const loaded = try fg.wip.load(access_kind, load_llvm_ty, payload_ptr, payload_alignment, "");
- const shifted = if (payload_llvm_ty != load_llvm_ty and o.target.cpu.arch.endian() == .Big)
+ const shifted = if (payload_llvm_ty != load_llvm_ty and o.target.cpu.arch.endian() == .big)
try fg.wip.bin(.lshr, loaded, try o.builder.intValue(
load_llvm_ty,
(payload_ty.abiSize(mod) - (std.math.divCeil(u64, payload_ty.bitSize(mod), 8) catch unreachable)) * 8,