Skip to content

Commit 2b05463

Browse files
authored
Add safe stdio rules (#261)
1 parent dd432a6 commit 2b05463

4 files changed

Lines changed: 1539 additions & 0 deletions

File tree

‎rules/stdio/tgt_refcount.rs‎

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,124 @@ fn f10() -> Ptr<::std::fs::File> {
9595
libcc2rs::cin()
9696
}
9797

98+
fn f11(a0: i32, a1: Ptr<::std::fs::File>) -> i32 {
99+
let __c = a0 as u8;
100+
match a1.with_mut(|__f| ::std::io::Write::write_all(__f, &[__c])) {
101+
Ok(()) => __c as i32,
102+
Err(__e) => {
103+
libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO));
104+
-1
105+
}
106+
}
107+
}
108+
109+
fn f12(a0: Ptr<u8>, a1: Ptr<::std::fs::File>) -> i32 {
110+
let __bytes: Vec<u8> = a0.to_c_string_iterator().collect();
111+
match a1.with_mut(|__f| ::std::io::Write::write_all(__f, &__bytes)) {
112+
Ok(()) => 0,
113+
Err(__e) => {
114+
libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO));
115+
-1
116+
}
117+
}
118+
}
119+
120+
fn f13(a0: Ptr<u8>) -> i32 {
121+
let __bytes: Vec<u8> = a0.to_c_string_iterator().collect();
122+
match libcc2rs::cout().with_mut(|__f| {
123+
::std::io::Write::write_all(__f, &__bytes)
124+
.and_then(|_| ::std::io::Write::write_all(__f, b"\n"))
125+
}) {
126+
Ok(()) => 0,
127+
Err(__e) => {
128+
libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO));
129+
-1
130+
}
131+
}
132+
}
133+
134+
fn f17(a0: Ptr<u8>, a1: i32, a2: Ptr<::std::fs::File>) -> Ptr<u8> {
135+
let __buf = a0.clone();
136+
let __n = a1;
137+
let __stream = a2.clone();
138+
if __n <= 0 {
139+
Ptr::null()
140+
} else {
141+
let __max = (__n - 1) as usize;
142+
let mut __dst = __buf.clone();
143+
let mut __count: usize = 0;
144+
let mut __failed = false;
145+
while __count < __max {
146+
let mut __b: [u8; 1] = [0];
147+
match __stream.with_mut(|__f| ::std::io::Read::read(__f, &mut __b)) {
148+
Ok(0) => break,
149+
Ok(_) => {
150+
__dst.write(__b[0]);
151+
__dst += 1;
152+
__count += 1;
153+
if __b[0] == b'\n' {
154+
break;
155+
}
156+
}
157+
Err(__e) => {
158+
if __e.kind() != ::std::io::ErrorKind::Interrupted {
159+
libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO));
160+
__failed = true;
161+
break;
162+
}
163+
}
164+
}
165+
}
166+
if __failed || __count == 0 {
167+
Ptr::null()
168+
} else {
169+
__dst.write(0);
170+
__buf
171+
}
172+
}
173+
}
174+
175+
fn f18(a0: Ptr<u8>, a1: Ptr<u8>, a2: Ptr<::std::fs::File>) -> Ptr<::std::fs::File> {
176+
let __stream = a2.clone();
177+
let __new = match a1.to_rust_string().as_str() {
178+
"rb" => ::std::fs::OpenOptions::new()
179+
.read(true)
180+
.open(a0.to_rust_string()),
181+
"wb" => ::std::fs::OpenOptions::new()
182+
.write(true)
183+
.create(true)
184+
.truncate(true)
185+
.open(a0.to_rust_string()),
186+
_ => panic!("unsupported mode"),
187+
};
188+
match __new {
189+
Ok(__f) => {
190+
__stream.write(__f);
191+
__stream
192+
}
193+
Err(__e) => {
194+
libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO));
195+
Ptr::null()
196+
}
197+
}
198+
}
199+
200+
fn f19(a0: Ptr<::std::fs::File>, a1: i64, a2: i32) -> i32 {
201+
let __r = a0.with_mut(|__f| match a2 {
202+
0 => ::std::io::Seek::seek(__f, ::std::io::SeekFrom::Start(a1 as u64)),
203+
1 => ::std::io::Seek::seek(__f, ::std::io::SeekFrom::Current(a1)),
204+
2 => ::std::io::Seek::seek(__f, ::std::io::SeekFrom::End(a1)),
205+
_ => Err(::std::io::Error::other("unsupported whence for fseeko.")),
206+
});
207+
match __r {
208+
Ok(_) => 0,
209+
Err(__e) => {
210+
libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EINVAL));
211+
-1
212+
}
213+
}
214+
}
215+
98216
fn f21(a0: Ptr<u8>, a1: usize, a2: Ptr<u8>, va: &[VaArg]) -> i32 {
99217
panic!(
100218
"snprintf is not supported in the refcount model (buf_is_null={}, size={}, fmt={:?}, varargs={})",
@@ -104,3 +222,30 @@ fn f21(a0: Ptr<u8>, a1: usize, a2: Ptr<u8>, va: &[VaArg]) -> i32 {
104222
va.len()
105223
)
106224
}
225+
226+
fn f22(a0: Ptr<u8>, a1: Ptr<u8>) -> i32 {
227+
match ::std::fs::rename(a0.to_rust_string(), a1.to_rust_string()) {
228+
Ok(()) => 0,
229+
Err(__e) => {
230+
libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO));
231+
-1
232+
}
233+
}
234+
}
235+
236+
fn f23(a0: Ptr<::std::fs::File>) -> i32 {
237+
let mut __b: [u8; 1] = [0];
238+
match a0.with_mut(|__f| ::std::io::Read::read(__f, &mut __b)) {
239+
Ok(0) => -1,
240+
Ok(_) => __b[0] as i32,
241+
Err(__e) => {
242+
libcc2rs::cpp2rust_errno().write(__e.raw_os_error().unwrap_or(::libc::EIO));
243+
-1
244+
}
245+
}
246+
}
247+
248+
fn f24(a0: Ptr<::std::fs::File>, a1: Ptr<u8>, a2: i32, a3: usize) -> i32 {
249+
/* std::fs::File is unbuffered */
250+
0
251+
}

0 commit comments

Comments
 (0)