fix u65529 and above overflowing in more places

See also #15537
This commit is contained in:
r00ster91 2023-05-28 01:04:26 +02:00
parent 5804f3f757
commit 11c32c756f
4 changed files with 13 additions and 13 deletions

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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