authorgravatar for r00ster91@proton.meWooster <r00ster91@proton.me> 2023-05-28 01:04:26+02:00
committergravatar for r00ster91@proton.meWooster <r00ster91@proton.me> 2023-06-27 19:08:24-04:00
log11c32c756f70540250139d6825937da53d74740a
tree81a2688fcbcc989384677192faccc264d5f07818
parent5804f3f75716ee77382880954cf73cd4005f33f7

fix u65529 and above overflowing in more places

See also #15537

4 files changed, 13 insertions(+), 13 deletions(-)

lib/std/io/reader.zig+5-5
......@@ -280,28 +280,28 @@ pub fn Reader(
280280
281281 /// Reads a native-endian integer
282282 pub fn readIntNative(self: Self, comptime T: type) (Error || error{EndOfStream})!T {
283 const bytes = try self.readBytesNoEof((@typeInfo(T).Int.bits + 7) / 8);
283 const bytes = try self.readBytesNoEof(@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8)));
284284 return mem.readIntNative(T, &bytes);
285285 }
286286
287287 /// Reads a foreign-endian integer
288288 pub fn readIntForeign(self: Self, comptime T: type) (Error || error{EndOfStream})!T {
289 const bytes = try self.readBytesNoEof((@typeInfo(T).Int.bits + 7) / 8);
289 const bytes = try self.readBytesNoEof(@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8)));
290290 return mem.readIntForeign(T, &bytes);
291291 }
292292
293293 pub fn readIntLittle(self: Self, comptime T: type) !T {
294 const bytes = try self.readBytesNoEof((@typeInfo(T).Int.bits + 7) / 8);
294 const bytes = try self.readBytesNoEof(@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8)));
295295 return mem.readIntLittle(T, &bytes);
296296 }
297297
298298 pub fn readIntBig(self: Self, comptime T: type) !T {
299 const bytes = try self.readBytesNoEof((@typeInfo(T).Int.bits + 7) / 8);
299 const bytes = try self.readBytesNoEof(@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8)));
300300 return mem.readIntBig(T, &bytes);
301301 }
302302
303303 pub fn readInt(self: Self, comptime T: type, endian: std.builtin.Endian) !T {
304 const bytes = try self.readBytesNoEof((@typeInfo(T).Int.bits + 7) / 8);
304 const bytes = try self.readBytesNoEof(@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8)));
305305 return mem.readInt(T, &bytes, endian);
306306 }
307307
lib/std/io/writer.zig+5-5
......@@ -47,32 +47,32 @@ pub fn Writer(
4747
4848 /// Write a native-endian integer.
4949 pub fn writeIntNative(self: Self, comptime T: type, value: T) Error!void {
50 var bytes: [(@typeInfo(T).Int.bits + 7) / 8]u8 = undefined;
50 var bytes: [@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8))]u8 = undefined;
5151 mem.writeIntNative(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value);
5252 return self.writeAll(&bytes);
5353 }
5454
5555 /// Write a foreign-endian integer.
5656 pub fn writeIntForeign(self: Self, comptime T: type, value: T) Error!void {
57 var bytes: [(@typeInfo(T).Int.bits + 7) / 8]u8 = undefined;
57 var bytes: [@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8))]u8 = undefined;
5858 mem.writeIntForeign(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value);
5959 return self.writeAll(&bytes);
6060 }
6161
6262 pub fn writeIntLittle(self: Self, comptime T: type, value: T) Error!void {
63 var bytes: [(@typeInfo(T).Int.bits + 7) / 8]u8 = undefined;
63 var bytes: [@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8))]u8 = undefined;
6464 mem.writeIntLittle(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value);
6565 return self.writeAll(&bytes);
6666 }
6767
6868 pub fn writeIntBig(self: Self, comptime T: type, value: T) Error!void {
69 var bytes: [(@typeInfo(T).Int.bits + 7) / 8]u8 = undefined;
69 var bytes: [@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8))]u8 = undefined;
7070 mem.writeIntBig(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value);
7171 return self.writeAll(&bytes);
7272 }
7373
7474 pub fn writeInt(self: Self, comptime T: type, value: T, endian: std.builtin.Endian) Error!void {
75 var bytes: [(@typeInfo(T).Int.bits + 7) / 8]u8 = undefined;
75 var bytes: [@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8))]u8 = undefined;
7676 mem.writeInt(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value, endian);
7777 return self.writeAll(&bytes);
7878 }
lib/std/mem.zig+1-1
......@@ -1616,7 +1616,7 @@ test "readIntBig and readIntLittle" {
16161616/// accepts any integer bit width.
16171617/// This function stores in native endian, which means it is implemented as a simple
16181618/// memory store.
1619pub fn writeIntNative(comptime T: type, buf: *[(@typeInfo(T).Int.bits + 7) / 8]u8, value: T) void {
1619pub fn writeIntNative(comptime T: type, buf: *[@as(u16, @intCast((@as(u17, @typeInfo(T).Int.bits) + 7) / 8))]u8, value: T) void {
16201620 @as(*align(1) T, @ptrCast(buf)).* = value;
16211621}
16221622
src/value.zig+2-2
......@@ -616,7 +616,7 @@ pub const Value = struct {
616616 .Int, .Enum => {
617617 const int_info = ty.intInfo(mod);
618618 const bits = int_info.bits;
619 const byte_count = (bits + 7) / 8;
619 const byte_count: u16 = @intCast((@as(u17, bits) + 7) / 8);
620620
621621 var bigint_buffer: BigIntSpace = undefined;
622622 const bigint = val.toBigInt(&bigint_buffer, mod);
......@@ -859,7 +859,7 @@ pub const Value = struct {
859859 };
860860 const int_info = int_ty.intInfo(mod);
861861 const bits = int_info.bits;
862 const byte_count = (bits + 7) / 8;
862 const byte_count: u16 = @intCast((@as(u17, bits) + 7) / 8);
863863 if (bits == 0 or buffer.len == 0) return mod.getCoerced(try mod.intValue(int_ty, 0), ty);
864864
865865 if (bits <= 64) switch (int_info.signedness) { // Fast path for integers <= u64