Skip to content

Commit 23a43ad

Browse files
committed
Add fstat and termios safe rules
1 parent 0cadc91 commit 23a43ad

3 files changed

Lines changed: 107 additions & 0 deletions

File tree

‎libcc2rs/src/libc_shims/termios.rs‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,68 @@ impl Clone for Termios {
4848

4949
impl ByteRepr for Termios {}
5050

51+
impl Termios {
52+
#[allow(clippy::unnecessary_cast)]
53+
pub fn from_libc(t: &::libc::termios) -> Self {
54+
let s = Self::default();
55+
*s.c_iflag.borrow_mut() = t.c_iflag as u32;
56+
*s.c_oflag.borrow_mut() = t.c_oflag as u32;
57+
*s.c_cflag.borrow_mut() = t.c_cflag as u32;
58+
*s.c_lflag.borrow_mut() = t.c_lflag as u32;
59+
#[cfg(target_os = "linux")]
60+
{
61+
*s.c_line.borrow_mut() = t.c_line;
62+
}
63+
{
64+
let mut cc = s.c_cc.borrow_mut();
65+
let n = t.c_cc.len().min(cc.len());
66+
cc[..n].copy_from_slice(&t.c_cc[..n]);
67+
}
68+
*s.c_ispeed.borrow_mut() = t.c_ispeed as u32;
69+
*s.c_ospeed.borrow_mut() = t.c_ospeed as u32;
70+
s
71+
}
72+
73+
#[cfg(target_os = "linux")]
74+
pub fn to_libc(&self) -> ::libc::termios {
75+
::libc::termios {
76+
c_iflag: *self.c_iflag.borrow(),
77+
c_oflag: *self.c_oflag.borrow(),
78+
c_cflag: *self.c_cflag.borrow(),
79+
c_lflag: *self.c_lflag.borrow(),
80+
c_line: *self.c_line.borrow(),
81+
c_cc: {
82+
let mut cc = [0u8; 32];
83+
let src = self.c_cc.borrow();
84+
let n = src.len().min(cc.len());
85+
cc[..n].copy_from_slice(&src[..n]);
86+
cc
87+
},
88+
c_ispeed: *self.c_ispeed.borrow(),
89+
c_ospeed: *self.c_ospeed.borrow(),
90+
}
91+
}
92+
93+
#[cfg(target_os = "macos")]
94+
pub fn to_libc(&self) -> ::libc::termios {
95+
::libc::termios {
96+
c_iflag: *self.c_iflag.borrow() as u64,
97+
c_oflag: *self.c_oflag.borrow() as u64,
98+
c_cflag: *self.c_cflag.borrow() as u64,
99+
c_lflag: *self.c_lflag.borrow() as u64,
100+
c_cc: {
101+
let mut cc = [0u8; 20];
102+
let src = self.c_cc.borrow();
103+
let n = src.len().min(cc.len());
104+
cc[..n].copy_from_slice(&src[..n]);
105+
cc
106+
},
107+
c_ispeed: *self.c_ispeed.borrow() as u64,
108+
c_ospeed: *self.c_ospeed.borrow() as u64,
109+
}
110+
}
111+
}
112+
51113
#[derive(Default)]
52114
pub struct Winsize {
53115
pub ws_row: Value<u16>,

‎rules/stat/tgt_refcount.rs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@ fn f1(a0: Ptr<u8>, a1: Ptr<Stat>) -> i32 {
2020
}
2121
}
2222

23+
fn f2(a0: i32, a1: Ptr<Stat>) -> i32 {
24+
match FdRegistry::with_fd(a0, |__fd| nix::sys::stat::fstat(__fd)) {
25+
Ok(__s) => {
26+
a1.with_mut(|__st| *__st = Stat::from_libc(&__s));
27+
0
28+
}
29+
Err(__e) => {
30+
libcc2rs::cpp2rust_errno().write(__e as i32);
31+
-1
32+
}
33+
}
34+
}
35+
2336
fn f3(a0: Ptr<u8>, a1: ::libc::mode_t) -> i32 {
2437
match nix::unistd::mkdir(
2538
a0.to_rust_string().as_str(),

‎rules/termios/tgt_refcount.rs‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,35 @@ fn t1() -> libcc2rs::Termios {
1010
fn t2() -> libcc2rs::Winsize {
1111
Default::default()
1212
}
13+
14+
fn f1(a0: i32, a1: i32, a2: Ptr<Termios>) -> i32 {
15+
let __action = match a1 {
16+
0 => nix::sys::termios::SetArg::TCSANOW,
17+
1 => nix::sys::termios::SetArg::TCSADRAIN,
18+
2 => nix::sys::termios::SetArg::TCSAFLUSH,
19+
__a => panic!("tcsetattr: unsupported action {__a}"),
20+
};
21+
let __t = nix::sys::termios::Termios::from(a2.with(|__src| __src.to_libc()));
22+
match FdRegistry::with_fd(a0, |__fd| {
23+
nix::sys::termios::tcsetattr(__fd, __action, &__t)
24+
}) {
25+
Ok(()) => 0,
26+
Err(__e) => {
27+
libcc2rs::cpp2rust_errno().write(__e as i32);
28+
-1
29+
}
30+
}
31+
}
32+
33+
fn f2(a0: i32, a1: Ptr<Termios>) -> i32 {
34+
match FdRegistry::with_fd(a0, |__fd| nix::sys::termios::tcgetattr(__fd)) {
35+
Ok(__t) => {
36+
a1.with_mut(|__dst| *__dst = Termios::from_libc(&__t.into()));
37+
0
38+
}
39+
Err(__e) => {
40+
libcc2rs::cpp2rust_errno().write(__e as i32);
41+
-1
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)