From 8a613e9e69b317886bc7096d6ccfd54ffc70d108 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Fri, 7 Feb 2025 14:47:28 +0100 Subject: [PATCH] Do not deselect applications if the application was not found --- dispatch/src/dispatch.rs | 19 ++++++++++--------- dispatch/tests/dispatch.rs | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/dispatch/src/dispatch.rs b/dispatch/src/dispatch.rs index a529897..1a9e1ef 100644 --- a/dispatch/src/dispatch.rs +++ b/dispatch/src/dispatch.rs @@ -8,6 +8,8 @@ //! //! Apps need to implement the App trait to be managed. //! +use core::mem; + use crate::response::SIZE as ResponseSize; use crate::App; use crate::{ @@ -381,14 +383,6 @@ impl<'pipe> ApduDispatch<'pipe> { // not necessarily the case for other apps // if there is a selected app with a different AID, deselect it - if let Some(current_aid) = self.current_aid { - if current_aid != aid { - let app = Self::find_app(self.current_aid.as_ref(), apps).unwrap(); - // for now all apps will be happy with this. - app.deselect(); - self.current_aid = None; - } - } // select specified app in any case if let Some(app) = Self::find_app(Some(&aid), apps) { @@ -401,7 +395,14 @@ impl<'pipe> ApduDispatch<'pipe> { _ => panic!("Unexpected buffer state."), }; - self.current_aid = Some(aid); + let old_aid = mem::replace(&mut self.current_aid, Some(aid)); + if let Some(old_aid) = old_aid { + if old_aid != aid { + let app = Self::find_app(self.current_aid.as_ref(), apps).unwrap(); + // for now all apps will be happy with this. + app.deselect(); + } + } self.handle_app_response(&result, &response); } else { diff --git a/dispatch/tests/dispatch.rs b/dispatch/tests/dispatch.rs index f65f037..236d34b 100644 --- a/dispatch/tests/dispatch.rs +++ b/dispatch/tests/dispatch.rs @@ -324,6 +324,25 @@ fn select_not_found() { ]) } +#[test] +#[serial] +fn select_not_found_still_selected() { + run_apdus(&[ + // Select + &hex!("00A40400 05 0A01000001"), + // Ok + &hex!("9000"), + // Select + &hex!("00A40400 05 0A01000100"), + // Not found + &hex!("6A82"), + // Echo app is still selected + &hex!("80100000 05 0102030405 00"), + // Echo + Ok + &hex!("0000000000 0102030405 9000"), + ]) +} + #[test] #[serial] fn select_bad_aid() {