Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an assertion to unix_term::read_bytes to prevent reading past the end of a buffer #225

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

bmoxb
Copy link

@bmoxb bmoxb commented Nov 6, 2024

The (marked safe) read_bytes function makes a call to the unsafe function libc::read. If read_bytes were to be called with a count value larger than the length of buf, the call to libc::read would be undefined behaviour and unsafe. All current callsites of read_bytes appear OK, but adding an assertion would ensure that all possible calls to read_bytes will be safe. If you would prefer to avoid a runtime assertion, I would suggest marking read_bytes as unsafe instead.

@@ -183,6 +183,7 @@ fn read_single_char(fd: i32) -> io::Result<Option<char>> {
// If successful, return the number of bytes read.
// Will return an error if nothing was read, i.e when called at end of file.
fn read_bytes(fd: i32, buf: &mut [u8], count: u8) -> io::Result<u8> {
assert!((count as usize) <= buf.len()); // Safety precondition - prevent reading past end of buffer.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the signature of this function is cursed. I've done a more thorough refactor in #223.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See b719bb4.

tamird added a commit to tamird/console that referenced this pull request Dec 17, 2024
Slices already encode their lengths; use that instead.

Remove the unused return value while I'm here.

Closes console-rs#225.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants