@@ -179,8 +179,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
179
179
}
180
180
181
181
/// Emulates calling a foreign item using its name, failing if the item is not supported.
182
- /// Returns Ok(false) if after calling this function, the call should return earlier instead of
183
- /// going to the next block .
182
+ /// Returns `true` if the caller is expected to jump to the return block, and `false` if
183
+ /// jumping has already been taken care of .
184
184
fn emulate_foreign_item_by_name (
185
185
& mut self ,
186
186
link_name : & str ,
@@ -315,35 +315,35 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
315
315
this. write_scalar ( Scalar :: from_int ( result, Size :: from_bits ( 32 ) ) , dest) ?;
316
316
}
317
317
318
- "memchr " => {
318
+ "memrchr " => {
319
319
let ptr = this. read_scalar ( args[ 0 ] ) ?. not_undef ( ) ?;
320
320
let val = this. read_scalar ( args[ 1 ] ) ?. to_i32 ( ) ? as u8 ;
321
321
let num = this. read_scalar ( args[ 2 ] ) ?. to_machine_usize ( this) ?;
322
- let idx = this
322
+ if let Some ( idx) = this
323
323
. memory
324
324
. read_bytes ( ptr, Size :: from_bytes ( num) ) ?
325
325
. iter ( )
326
- . position ( |& c| c == val) ;
327
- if let Some ( idx) = idx {
328
- let new_ptr = ptr. ptr_offset ( Size :: from_bytes ( idx as u64 ) , this) ?;
326
+ . rev ( )
327
+ . position ( |& c| c == val)
328
+ {
329
+ let new_ptr = ptr. ptr_offset ( Size :: from_bytes ( num - idx as u64 - 1 ) , this) ?;
329
330
this. write_scalar ( new_ptr, dest) ?;
330
331
} else {
331
332
this. write_null ( dest) ?;
332
333
}
333
334
}
334
335
335
- "memrchr " => {
336
+ "memchr " => {
336
337
let ptr = this. read_scalar ( args[ 0 ] ) ?. not_undef ( ) ?;
337
338
let val = this. read_scalar ( args[ 1 ] ) ?. to_i32 ( ) ? as u8 ;
338
339
let num = this. read_scalar ( args[ 2 ] ) ?. to_machine_usize ( this) ?;
339
- if let Some ( idx) = this
340
+ let idx = this
340
341
. memory
341
342
. read_bytes ( ptr, Size :: from_bytes ( num) ) ?
342
343
. iter ( )
343
- . rev ( )
344
- . position ( |& c| c == val)
345
- {
346
- let new_ptr = ptr. ptr_offset ( Size :: from_bytes ( num - idx as u64 - 1 ) , this) ?;
344
+ . position ( |& c| c == val) ;
345
+ if let Some ( idx) = idx {
346
+ let new_ptr = ptr. ptr_offset ( Size :: from_bytes ( idx as u64 ) , this) ?;
347
347
this. write_scalar ( new_ptr, dest) ?;
348
348
} else {
349
349
this. write_null ( dest) ?;
0 commit comments