Skip to content

Commit 6fde554

Browse files
authored
Remove unsafe and unnecessary size argument from FileDesc::read() (#821)
The `size` argument to `FileDesc::read()` is not checked against the length of the buffer, so `libc::read()` could end up writing past the buffer if we passed a size that's too large. However, we always pass exactly the size of the buffer, so that doesn't happen. Let's just remove the argument since it's not currently needed, thereby removing the risk of bugs if the function is used incorrectly by future callers. This came up in review of `unsafe` Rust code at my company.
1 parent f54e937 commit 6fde554

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

Diff for: src/event/source/unix/mio.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl EventSource for UnixInternalEventSource {
9393
match token {
9494
TTY_TOKEN => {
9595
loop {
96-
match self.tty_fd.read(&mut self.tty_buffer, TTY_BUFFER_SIZE) {
96+
match self.tty_fd.read(&mut self.tty_buffer) {
9797
Ok(read_count) => {
9898
if read_count > 0 {
9999
self.parser.advance(

Diff for: src/terminal/sys/file_descriptor.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ impl FileDesc {
2929
FileDesc { fd, close_on_drop }
3030
}
3131

32-
pub fn read(&self, buffer: &mut [u8], size: usize) -> io::Result<usize> {
32+
pub fn read(&self, buffer: &mut [u8]) -> io::Result<usize> {
3333
let result = unsafe {
3434
libc::read(
3535
self.fd,
3636
buffer.as_mut_ptr() as *mut libc::c_void,
37-
size as size_t,
37+
buffer.len() as size_t,
3838
)
3939
};
4040

0 commit comments

Comments
 (0)