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(...@@ -280,28 +280,28 @@ pub fn Reader(
280280
281 /// Reads a native-endian integer281 /// Reads a native-endian integer
282 pub fn readIntNative(self: Self, comptime T: type) (Error || error{EndOfStream})!T {282 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)));
284 return mem.readIntNative(T, &bytes);284 return mem.readIntNative(T, &bytes);
285 }285 }
286286
287 /// Reads a foreign-endian integer287 /// Reads a foreign-endian integer
288 pub fn readIntForeign(self: Self, comptime T: type) (Error || error{EndOfStream})!T {288 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)));
290 return mem.readIntForeign(T, &bytes);290 return mem.readIntForeign(T, &bytes);
291 }291 }
292292
293 pub fn readIntLittle(self: Self, comptime T: type) !T {293 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)));
295 return mem.readIntLittle(T, &bytes);295 return mem.readIntLittle(T, &bytes);
296 }296 }
297297
298 pub fn readIntBig(self: Self, comptime T: type) !T {298 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)));
300 return mem.readIntBig(T, &bytes);300 return mem.readIntBig(T, &bytes);
301 }301 }
302302
303 pub fn readInt(self: Self, comptime T: type, endian: std.builtin.Endian) !T {303 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)));
305 return mem.readInt(T, &bytes, endian);305 return mem.readInt(T, &bytes, endian);
306 }306 }
307307
lib/std/io/writer.zig+5-5
...@@ -47,32 +47,32 @@ pub fn Writer(...@@ -47,32 +47,32 @@ pub fn Writer(
4747
48 /// Write a native-endian integer.48 /// Write a native-endian integer.
49 pub fn writeIntNative(self: Self, comptime T: type, value: T) Error!void {49 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;
51 mem.writeIntNative(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value);51 mem.writeIntNative(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value);
52 return self.writeAll(&bytes);52 return self.writeAll(&bytes);
53 }53 }
5454
55 /// Write a foreign-endian integer.55 /// Write a foreign-endian integer.
56 pub fn writeIntForeign(self: Self, comptime T: type, value: T) Error!void {56 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;
58 mem.writeIntForeign(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value);58 mem.writeIntForeign(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value);
59 return self.writeAll(&bytes);59 return self.writeAll(&bytes);
60 }60 }
6161
62 pub fn writeIntLittle(self: Self, comptime T: type, value: T) Error!void {62 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;
64 mem.writeIntLittle(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value);64 mem.writeIntLittle(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value);
65 return self.writeAll(&bytes);65 return self.writeAll(&bytes);
66 }66 }
6767
68 pub fn writeIntBig(self: Self, comptime T: type, value: T) Error!void {68 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;
70 mem.writeIntBig(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value);70 mem.writeIntBig(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value);
71 return self.writeAll(&bytes);71 return self.writeAll(&bytes);
72 }72 }
7373
74 pub fn writeInt(self: Self, comptime T: type, value: T, endian: std.builtin.Endian) Error!void {74 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;
76 mem.writeInt(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value, endian);76 mem.writeInt(std.math.ByteAlignedInt(@TypeOf(value)), &bytes, value, endian);
77 return self.writeAll(&bytes);77 return self.writeAll(&bytes);
78 }78 }
lib/std/mem.zig+1-1
...@@ -1616,7 +1616,7 @@ test "readIntBig and readIntLittle" {...@@ -1616,7 +1616,7 @@ test "readIntBig and readIntLittle" {
1616/// accepts any integer bit width.1616/// accepts any integer bit width.
1617/// This function stores in native endian, which means it is implemented as a simple1617/// This function stores in native endian, which means it is implemented as a simple
1618/// memory store.1618/// 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 {
1620 @as(*align(1) T, @ptrCast(buf)).* = value;1620 @as(*align(1) T, @ptrCast(buf)).* = value;
1621}1621}
16221622
src/value.zig+2-2
...@@ -616,7 +616,7 @@ pub const Value = struct {...@@ -616,7 +616,7 @@ pub const Value = struct {
616 .Int, .Enum => {616 .Int, .Enum => {
617 const int_info = ty.intInfo(mod);617 const int_info = ty.intInfo(mod);
618 const bits = int_info.bits;618 const bits = int_info.bits;
619 const byte_count = (bits + 7) / 8;619 const byte_count: u16 = @intCast((@as(u17, bits) + 7) / 8);
620620
621 var bigint_buffer: BigIntSpace = undefined;621 var bigint_buffer: BigIntSpace = undefined;
622 const bigint = val.toBigInt(&bigint_buffer, mod);622 const bigint = val.toBigInt(&bigint_buffer, mod);
...@@ -859,7 +859,7 @@ pub const Value = struct {...@@ -859,7 +859,7 @@ pub const Value = struct {
859 };859 };
860 const int_info = int_ty.intInfo(mod);860 const int_info = int_ty.intInfo(mod);
861 const bits = int_info.bits;861 const bits = int_info.bits;
862 const byte_count = (bits + 7) / 8;862 const byte_count: u16 = @intCast((@as(u17, bits) + 7) / 8);
863 if (bits == 0 or buffer.len == 0) return mod.getCoerced(try mod.intValue(int_ty, 0), ty);863 if (bits == 0 or buffer.len == 0) return mod.getCoerced(try mod.intValue(int_ty, 0), ty);
864864
865 if (bits <= 64) switch (int_info.signedness) { // Fast path for integers <= u64865 if (bits <= 64) switch (int_info.signedness) { // Fast path for integers <= u64