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

Use from_bits_retain for all Termios wrapper fields. #2253

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/2253.fixed.md
@@ -0,0 +1 @@
Fixed roundtripping of libc `termios` with unknown flags through `Termios` wrapper.
36 changes: 29 additions & 7 deletions src/sys/termios.rs
Expand Up @@ -219,10 +219,10 @@ impl Termios {
/// Updates the wrapper values from the internal `libc::termios` data structure.
pub(crate) fn update_wrapper(&mut self) {
let termios = *self.inner.borrow_mut();
self.input_flags = InputFlags::from_bits_truncate(termios.c_iflag);
self.output_flags = OutputFlags::from_bits_truncate(termios.c_oflag);
self.input_flags = InputFlags::from_bits_retain(termios.c_iflag);
self.output_flags = OutputFlags::from_bits_retain(termios.c_oflag);
self.control_flags = ControlFlags::from_bits_retain(termios.c_cflag);
self.local_flags = LocalFlags::from_bits_truncate(termios.c_lflag);
self.local_flags = LocalFlags::from_bits_retain(termios.c_lflag);
self.control_chars = termios.c_cc;
#[cfg(any(linux_android, target_os = "haiku"))]
{
Expand All @@ -235,10 +235,10 @@ impl From<libc::termios> for Termios {
fn from(termios: libc::termios) -> Self {
Termios {
inner: RefCell::new(termios),
input_flags: InputFlags::from_bits_truncate(termios.c_iflag),
output_flags: OutputFlags::from_bits_truncate(termios.c_oflag),
control_flags: ControlFlags::from_bits_truncate(termios.c_cflag),
local_flags: LocalFlags::from_bits_truncate(termios.c_lflag),
input_flags: InputFlags::from_bits_retain(termios.c_iflag),
output_flags: OutputFlags::from_bits_retain(termios.c_oflag),
control_flags: ControlFlags::from_bits_retain(termios.c_cflag),
local_flags: LocalFlags::from_bits_retain(termios.c_lflag),
control_chars: termios.c_cc,
#[cfg(any(linux_android, target_os = "haiku"))]
line_discipline: termios.c_line,
Expand Down Expand Up @@ -932,4 +932,26 @@ mod test {
#[cfg(target_os = "haiku")]
BaudRate::try_from(99).expect_err("assertion failed");
}

#[test]
fn roundtrip_termios() {
// A fake termios including flag bits which we don't recognise.
#[allow(clippy::needless_update)]
let original = libc::termios {
c_iflag: 0xf00f,
c_oflag: 0xd00d,
c_cflag: 0x6642,
c_lflag: 0x1234,
c_cc: [0; NCCS],
..unsafe { std::mem::zeroed() }
};

let mut attrs: Termios = original.into();
let before_update = attrs.get_libc_termios().to_owned();
attrs.update_wrapper();
let after_update = attrs.get_libc_termios().to_owned();

assert_eq!(before_update, original);
assert_eq!(after_update, original);
}
}