Skip to content

Commit 19e0b16

Browse files
committed
uefi: serial: fix read()
The old way is not just very inflexible but also incorrect and error prone.
1 parent 8cbc3c0 commit 19e0b16

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

uefi-test-runner/src/proto/console/serial.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ fn serial_test_helper(serial: &mut Serial) -> Result {
2929
serial.write(OUTPUT).discard_errdata()?;
3030

3131
let mut input = [0u8; MSG_LEN];
32-
serial.read(&mut input).discard_errdata()?;
32+
let n = serial.read(&mut input).discard_errdata()?;
33+
assert_eq!(n, input.len());
3334

3435
// Clean up after ourselves
3536
serial.reset()?;

uefi/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@
6060
getting stabilized in stable Rust.
6161
- Removed `File::get_boxed_info_in`
6262
- Removed `Directory::read_entry_boxed_in`
63-
63+
- Fixed `Serial::read` to properly read only as many bytes as it can report
64+
that `n` to the caller.
6465

6566
# uefi - 0.35.0 (2025-05-04)
6667

uefi/src/proto/console/serial.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,16 @@ impl Serial {
8181

8282
/// Reads data from this device.
8383
///
84-
/// This operation will block until the buffer has been filled with data or
85-
/// an error occurs. In the latter case, the error will indicate how many
86-
/// bytes were actually read from the device.
87-
pub fn read(&mut self, data: &mut [u8]) -> Result<(), usize> {
84+
/// This operation will block until the buffer has been filled with data,
85+
/// all data was read, or error occurs. In any case, this function will
86+
/// return the number of read bytes.
87+
pub fn read(
88+
&mut self,
89+
data: &mut [u8],
90+
) -> Result<usize /* read bytes*/, usize /* read bytes*/> {
8891
let mut buffer_size = data.len();
89-
unsafe { (self.0.read)(&mut self.0, &mut buffer_size, data.as_mut_ptr()) }.to_result_with(
90-
|| debug_assert_eq!(buffer_size, data.len()),
91-
|_| buffer_size,
92-
)
92+
unsafe { (self.0.read)(&mut self.0, &mut buffer_size, data.as_mut_ptr()) }
93+
.to_result_with(|| buffer_size, |_| buffer_size)
9394
}
9495

9596
/// Writes data to this device.

0 commit comments

Comments
 (0)