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

embedded_io::serial::Reader implementations drop data on error #782

Closed
jannic opened this issue Mar 16, 2024 · 0 comments · Fixed by #798
Closed

embedded_io::serial::Reader implementations drop data on error #782

jannic opened this issue Mar 16, 2024 · 0 comments · Fixed by #798

Comments

@jannic
Copy link
Member

jannic commented Mar 16, 2024

While working on #781 I noticed a flaw in the error handling of the reader implementation:

impl<D: UartDevice, P: ValidUartPinout<D>> embedded_io::Read for UartPeripheral<Enabled, D, P> {
    fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
        nb::block!(self.read_raw(buf)).map_err(|e| e.err_type)
    }
}

The error returned by read_raw does not only contain the error itself (in err_type) but also pub discarded: &'err [u8], possibly data that was already read from the peripheral before the error occurred. That data gets silently dropped.

Instead, it may be better to check if the length of discarded is non-zero, and if so, return an Ok result containing that data. In that case, the error needs to be stored somewhere, to be returned in the next call to Read.

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 a pull request may close this issue.

1 participant