From 11c32c756f70540250139d6825937da53d74740a Mon Sep 17 00:00:00 2001 From: r00ster91 Date: Sun, 28 May 2023 01:04:26 +0200 Subject: [PATCH] fix u65529 and above overflowing in more places See also #15537 --- lib/std/io/reader.zig | 10 +++++----- lib/std/io/writer.zig | 10 +++++----- lib/std/mem.zig | 2 +- src/value.zig | 4 ++-- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/std/io/reader.zig b/lib/std/io/reader.zig index 4dde51838b..a0490fee9d 100644 --- a/lib/std/io/reader.zig +++ b/lib/std/io/reader.zig @@ -280,28 +280,28 @@ pub fn Reader( /// Reads a native-endian integer pub fn readIntNative(self: Self, comptime T: type) (Error || error{EndOfStream})!T { - const bytes = try self.readBytesNoEof((@typeInfo(T).Int.bits + 7) / 8); + const bytes = try self.readBytesNoEof(@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8))); return mem.readIntNative(T, &bytes); } /// Reads a foreign-endian integer pub fn readIntForeign(self: Self, comptime T: type) (Error || error{EndOfStream})!T { - const bytes = try self.readBytesNoEof((@typeInfo(T).Int.bits + 7) / 8); + const bytes = try self.readBytesNoEof(@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8))); return mem.readIntForeign(T, &bytes); } pub fn readIntLittle(self: Self, comptime T: type) !T { - const bytes = try self.readBytesNoEof((@typeInfo(T).Int.bits + 7) / 8); + const bytes = try self.readBytesNoEof(@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8))); return mem.readIntLittle(T, &bytes); } pub fn readIntBig(self: Self, comptime T: type) !T { - const bytes = try self.readBytesNoEof((@typeInfo(T).Int.bits + 7) / 8); + const bytes = try self.readBytesNoEof(@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8))); return mem.readIntBig(T, &bytes); } pub fn readInt(self: Self, comptime T: type, endian: std.builtin.Endian) !T { - const bytes = try self.readBytesNoEof((@typeInfo(T).Int.bits + 7) / 8); + const bytes = try self.readBytesNoEof(@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8))); return mem.readInt(T, &bytes, endian); } diff --git a/lib/std/io/writer.zig b/lib/std/io/writer.zig index d0b7fa11ee..41dcf9b6e7 100644 --- a/lib/std/io/writer.zig +++ b/lib/std/io/writer.zig @@ -47,32 +47,32 @@ pub fn Writer( /// Write a native-endian integer. pub fn writeIntNative(self: Self, comptime T: type, value: T) Error!void { - var bytes: [(@typeInfo(T).Int.bits + 7) / 8]u8 = undefined; + var bytes: [@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8))]u8 = undefined; mem.writeIntNative(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value); return self.writeAll(&bytes); } /// Write a foreign-endian integer. pub fn writeIntForeign(self: Self, comptime T: type, value: T) Error!void { - var bytes: [(@typeInfo(T).Int.bits + 7) / 8]u8 = undefined; + var bytes: [@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8))]u8 = undefined; mem.writeIntForeign(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value); return self.writeAll(&bytes); } pub fn writeIntLittle(self: Self, comptime T: type, value: T) Error!void { - var bytes: [(@typeInfo(T).Int.bits + 7) / 8]u8 = undefined; + var bytes: [@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8))]u8 = undefined; mem.writeIntLittle(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value); return self.writeAll(&bytes); } pub fn writeIntBig(self: Self, comptime T: type, value: T) Error!void { - var bytes: [(@typeInfo(T).Int.bits + 7) / 8]u8 = undefined; + var bytes: [@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8))]u8 = undefined; mem.writeIntBig(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value); return self.writeAll(&bytes); } pub fn writeInt(self: Self, comptime T: type, value: T, endian: std.builtin.Endian) Error!void { - var bytes: [(@typeInfo(T).Int.bits + 7) / 8]u8 = undefined; + var bytes: [@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8))]u8 = undefined; mem.writeInt(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value, endian); return self.writeAll(&bytes); } diff --git a/lib/std/mem.zig b/lib/std/mem.zig index d3e874a211..489fec7ea1 100644 --- a/lib/std/mem.zig +++ b/lib/std/mem.zig @@ -1616,7 +1616,7 @@ test "readIntBig and readIntLittle" { /// accepts any integer bit width. /// This function stores in native endian, which means it is implemented as a simple /// memory store. -pub fn writeIntNative(comptime T: type, buf: *[(@typeInfo(T).Int.bits + 7) / 8]u8, value: T) void { +pub fn writeIntNative(comptime T: type, buf: *[@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8))]u8, value: T) void { @as(*align(1) T, @ptrCast(buf)).* = value; } diff --git a/src/value.zig b/src/value.zig index 6b85ebd552..af2bf209cc 100644 --- a/src/value.zig +++ b/src/value.zig @@ -616,7 +616,7 @@ pub const Value = struct { .Int, .Enum => { const int_info = ty.intInfo(mod); const bits = int_info.bits; - const byte_count = (bits + 7) / 8; + const byte_count: u16 = @intCast((@as(u17, bits) + 7) / 8); var bigint_buffer: BigIntSpace = undefined; const bigint = val.toBigInt(&bigint_buffer, mod); @@ -859,7 +859,7 @@ pub const Value = struct { }; const int_info = int_ty.intInfo(mod); const bits = int_info.bits; - const byte_count = (bits + 7) / 8; + const byte_count: u16 = @intCast((@as(u17, bits) + 7) / 8); if (bits == 0 or buffer.len == 0) return mod.getCoerced(try mod.intValue(int_ty, 0), ty); if (bits <= 64) switch (int_info.signedness) { // Fast path for integers <= u64