@@ -48,21 +48,16 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
48
48
. memory ( )
49
49
. read_c_str ( this. read_scalar ( path_op) ?. not_undef ( ) ?) ?;
50
50
let path = std:: str:: from_utf8 ( path_bytes)
51
- . map_err ( |_| err_unsup_format ! ( "{:?} is not a valid utf-8 string" , path_bytes) ) ?;
52
-
53
- match File :: open ( path) {
54
- Ok ( file) => {
55
- let mut fh = & mut this. machine . file_handler ;
56
- fh. low += 1 ;
57
- fh. handles . insert ( fh. low , FileHandle { file, flag} ) ;
58
- Ok ( fh. low )
59
- }
60
-
61
- Err ( e) => {
62
- this. machine . last_error = e. raw_os_error ( ) . unwrap ( ) as u32 ;
63
- Ok ( -1 )
64
- }
65
- }
51
+ . map_err ( |_| err_unsup_format ! ( "{:?} is not a valid utf-8 string" , path_bytes) ) ?
52
+ . to_owned ( ) ;
53
+ let fd = File :: open ( & path) . map ( |file| {
54
+ let mut fh = & mut this. machine . file_handler ;
55
+ fh. low += 1 ;
56
+ fh. handles . insert ( fh. low , FileHandle { file, flag} ) ;
57
+ fh. low
58
+ } ) ;
59
+
60
+ this. consume_result :: < i32 > ( fd, -1 )
66
61
}
67
62
68
63
fn fcntl (
@@ -116,13 +111,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
116
111
let fd = this. read_scalar ( fd_op) ?. to_i32 ( ) ?;
117
112
118
113
if let Some ( handle) = this. machine . file_handler . handles . remove ( & fd) {
119
- match handle. file . sync_all ( ) {
120
- Ok ( ( ) ) => Ok ( 0 ) ,
121
- Err ( e) => {
122
- this. machine . last_error = e. raw_os_error ( ) . unwrap ( ) as u32 ;
123
- Ok ( -1 )
124
- }
125
- }
114
+ this. consume_result :: < i32 > ( handle. file . sync_all ( ) . map ( |_| 0 ) , -1 )
126
115
} else {
127
116
this. machine . last_error = this. eval_libc_i32 ( "EBADF" ) ? as u32 ;
128
117
Ok ( -1 )
@@ -147,26 +136,32 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
147
136
let buf = this. force_ptr ( this. read_scalar ( buf_op) ?. not_undef ( ) ?) ?;
148
137
let count = this. read_scalar ( count_op) ?. to_usize ( & * this. tcx ) ?;
149
138
150
- if let Some ( FileHandle { file, ..} ) = this. machine . file_handler . handles . get_mut ( & fd) {
151
- let mut bytes = vec ! [ 0 ; count as usize ] ;
152
- match file. read ( & mut bytes) {
153
- Ok ( read_bytes) => {
154
- bytes. truncate ( read_bytes) ;
155
-
156
- this. memory_mut ( )
157
- . get_mut ( buf. alloc_id ) ?
158
- . write_bytes ( tcx, buf, & bytes) ?;
139
+ let mut bytes = vec ! [ 0 ; count as usize ] ;
159
140
160
- Ok ( read_bytes as i64 )
161
- }
162
- Err ( e) => {
163
- this. machine . last_error = e. raw_os_error ( ) . unwrap ( ) as u32 ;
164
- Ok ( -1 )
165
- }
166
- }
141
+ let read_result = if let Some ( FileHandle { file, ..} ) = this. machine . file_handler . handles . get_mut ( & fd) {
142
+ file. read ( & mut bytes) . map ( |bytes| bytes as i64 )
167
143
} else {
168
144
this. machine . last_error = this. eval_libc_i32 ( "EBADF" ) ? as u32 ;
169
- Ok ( -1 )
145
+ return Ok ( -1 ) ;
146
+ } ;
147
+
148
+ let read_bytes = this. consume_result :: < i64 > ( read_result, -1 ) ?;
149
+ if read_bytes != -1 {
150
+ bytes. truncate ( read_bytes as usize ) ;
151
+ this. memory_mut ( )
152
+ . get_mut ( buf. alloc_id ) ?
153
+ . write_bytes ( tcx, buf, & bytes) ?;
154
+ }
155
+ Ok ( read_bytes)
156
+ }
157
+
158
+ fn consume_result < T > ( & mut self , result : std:: io:: Result < T > , t : T ) -> InterpResult < ' tcx , T > {
159
+ match result {
160
+ Ok ( ok) => Ok ( ok) ,
161
+ Err ( e) => {
162
+ self . eval_context_mut ( ) . machine . last_error = e. raw_os_error ( ) . unwrap ( ) as u32 ;
163
+ Ok ( t)
164
+ }
170
165
}
171
166
}
172
167
}
0 commit comments