From 9b56c7de79ee5aa4540f5427a37fd8883d1fb4e6 Mon Sep 17 00:00:00 2001 From: lockbox Date: Sun, 23 Jul 2023 13:24:43 -0400 Subject: [PATCH] Fix type mismatch for Reader.readIntoBoundedBytes (#16416) - add unit test to verify .readIntoBoundedBytes behavior - add unit test to verify .readBoundedBytes behavior --- lib/std/io/reader.zig | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/lib/std/io/reader.zig b/lib/std/io/reader.zig index a0490fee9d..0a02e17b32 100644 --- a/lib/std/io/reader.zig +++ b/lib/std/io/reader.zig @@ -257,17 +257,23 @@ pub fn Reader( return bytes; } - /// Reads bytes into the bounded array, until - /// the bounded array is full, or the stream ends. + /// Reads bytes until `bounded.len` is equal to `num_bytes`, + /// or the stream ends. + /// + /// * it is assumed that `num_bytes` will not exceed `bounded.capacity()` pub fn readIntoBoundedBytes( self: Self, comptime num_bytes: usize, bounded: *std.BoundedArray(u8, num_bytes), ) Error!void { while (bounded.len < num_bytes) { + // get at most the number of bytes free in the bounded array const bytes_read = try self.read(bounded.unusedCapacitySlice()); if (bytes_read == 0) return; - bounded.len += bytes_read; + + // bytes_read will never be larger than @TypeOf(bounded.len) + // due to `self.read` being bounded by `bounded.unusedCapacitySlice()` + bounded.len += @as(@TypeOf(bounded.len), @intCast(bytes_read)); } } @@ -728,3 +734,24 @@ test "Reader.streamUntilDelimiter writes all bytes without delimiter to the outp try std.testing.expectError(error.StreamTooLong, reader.streamUntilDelimiter(writer, '!', 5)); } + +test "Reader.readBoundedBytes correctly reads into a new bounded array" { + const test_string = "abcdefg"; + var fis = std.io.fixedBufferStream(test_string); + const reader = fis.reader(); + + var array = try reader.readBoundedBytes(10000); + try testing.expectEqualStrings(array.slice(), test_string); +} + +test "Reader.readIntoBoundedBytes correctly reads into a provided bounded array" { + const test_string = "abcdefg"; + var fis = std.io.fixedBufferStream(test_string); + const reader = fis.reader(); + + var bounded_array = std.BoundedArray(u8, 10000){}; + + // compile time error if the size is not the same at the provided `bounded.capacity()` + try reader.readIntoBoundedBytes(10000, &bounded_array); + try testing.expectEqualStrings(bounded_array.slice(), test_string); +}