Skip to content

Commit 228013c

Browse files
Do not deselect applications if the application was not found
1 parent 7e68a8d commit 228013c

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

dispatch/src/dispatch.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
//!
99
//! Apps need to implement the App trait to be managed.
1010
//!
11+
use core::mem;
12+
1113
use crate::response::SIZE as ResponseSize;
1214
use crate::App;
1315
use crate::{
@@ -381,14 +383,6 @@ impl<'pipe> ApduDispatch<'pipe> {
381383
// not necessarily the case for other apps
382384

383385
// if there is a selected app with a different AID, deselect it
384-
if let Some(current_aid) = self.current_aid {
385-
if current_aid != aid {
386-
let app = Self::find_app(self.current_aid.as_ref(), apps).unwrap();
387-
// for now all apps will be happy with this.
388-
app.deselect();
389-
self.current_aid = None;
390-
}
391-
}
392386

393387
// select specified app in any case
394388
if let Some(app) = Self::find_app(Some(&aid), apps) {
@@ -401,7 +395,14 @@ impl<'pipe> ApduDispatch<'pipe> {
401395
_ => panic!("Unexpected buffer state."),
402396
};
403397

404-
self.current_aid = Some(aid);
398+
let old_aid = mem::replace(&mut self.current_aid, Some(aid));
399+
if let Some(old_aid) = old_aid {
400+
if old_aid != aid {
401+
let app = Self::find_app(self.current_aid.as_ref(), apps).unwrap();
402+
// for now all apps will be happy with this.
403+
app.deselect();
404+
}
405+
}
405406

406407
self.handle_app_response(&result, &response);
407408
} else {

dispatch/tests/dispatch.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,25 @@ fn select_not_found() {
324324
])
325325
}
326326

327+
#[test]
328+
#[serial]
329+
fn select_not_found_still_selected() {
330+
run_apdus(&[
331+
// Select
332+
&hex!("00A40400 05 0A01000001"),
333+
// Ok
334+
&hex!("9000"),
335+
// Select
336+
&hex!("00A40400 05 0A01000100"),
337+
// Not found
338+
&hex!("6A82"),
339+
// Echo app is still selected
340+
&hex!("80100000 05 0102030405 00"),
341+
// Echo + Ok
342+
&hex!("0000000000 0102030405 9000"),
343+
])
344+
}
345+
327346
#[test]
328347
#[serial]
329348
fn select_bad_aid() {

0 commit comments

Comments
 (0)