From 53cc8edcfde9729827b97d147e7f1c8f67c379b6 Mon Sep 17 00:00:00 2001 From: yyc12345 Date: Mon, 18 May 2026 17:04:11 +0800 Subject: [PATCH] fix: update blank guard usage and fix issue --- example/manifest/ppic.toml | 1 + wfassoc-exec/src/runner.rs | 1 - wfassoc/src/lowlevel/app_path_key.rs | 3 +- wfassoc/src/lowlevel/applications_key.rs | 66 ++++++++++-------------- wfassoc/src/lowlevel/ext_key.rs | 50 +++++++++--------- wfassoc/src/lowlevel/progid_key.rs | 34 +++++------- wfassoc/src/win32/regext.rs | 3 ++ 7 files changed, 68 insertions(+), 90 deletions(-) diff --git a/example/manifest/ppic.toml b/example/manifest/ppic.toml index b4a037a..691caf5 100644 --- a/example/manifest/ppic.toml +++ b/example/manifest/ppic.toml @@ -77,6 +77,7 @@ default = '@C:\path\to\ppic.exe,-1001' # And more string resources... jpg = '@C:\path\to\ppic.exe,-1011' +jfif = '@C:\path\to\ppic.exe,-1050' gif = '@C:\path\to\ppic.exe,-1012' bmp = '@C:\path\to\ppic.exe,-1013' png = '@C:\path\to\ppic.exe,-1014' diff --git a/wfassoc-exec/src/runner.rs b/wfassoc-exec/src/runner.rs index f0de4c6..e4bf922 100644 --- a/wfassoc-exec/src/runner.rs +++ b/wfassoc-exec/src/runner.rs @@ -168,7 +168,6 @@ fn run_ext_list( pub fn run(c: cli::Cli) -> Result<()> { // Read manifest file first let mf = manifest::Manifest::from_file(Path::new(&c.manifest_file))?; - println!("{:?}", mf); // Parse it into schema let schema = mf.into_schema()?; // Parse it into program diff --git a/wfassoc/src/lowlevel/app_path_key.rs b/wfassoc/src/lowlevel/app_path_key.rs index 0884ec9..b47b3f7 100644 --- a/wfassoc/src/lowlevel/app_path_key.rs +++ b/wfassoc/src/lowlevel/app_path_key.rs @@ -39,8 +39,7 @@ impl AppPathsKey { OpenKeyTerritory::Hybrid => panic!("unexpected hybrid key territory"), }; // navigate to App Paths - let app_paths = - hk.open_subkey_with_flags(regext::blank_path_guard(Self::APP_PATHS)?, perms)?; + let app_paths = hk.open_subkey_with_flags(Self::APP_PATHS, perms)?; // open file name key if possible let this_app = regext::try_open_subkey_with_flags( &app_paths, diff --git a/wfassoc/src/lowlevel/applications_key.rs b/wfassoc/src/lowlevel/applications_key.rs index d826732..59594b6 100644 --- a/wfassoc/src/lowlevel/applications_key.rs +++ b/wfassoc/src/lowlevel/applications_key.rs @@ -40,14 +40,12 @@ impl ApplicationsKey { OpenKeyTerritory::Hybrid => RegKey::predef(HKEY_CLASSES_ROOT), }; let applications = match territory { - OpenKeyTerritory::User | OpenKeyTerritory::System => hk.open_subkey_with_flags( - regext::blank_path_guard(Self::FULL_APPLICATIONS)?, - perms, - )?, - OpenKeyTerritory::Hybrid => hk.open_subkey_with_flags( - regext::blank_path_guard(Self::PARTIAL_APPLICATIONS)?, - perms, - )?, + OpenKeyTerritory::User | OpenKeyTerritory::System => { + hk.open_subkey_with_flags(Self::FULL_APPLICATIONS, perms)? + } + OpenKeyTerritory::Hybrid => { + hk.open_subkey_with_flags(Self::PARTIAL_APPLICATIONS, perms)? + } }; // open app key if possible let this_app = regext::try_open_subkey_with_flags( @@ -117,7 +115,7 @@ impl ApplicationsKey { // Get shell subkey let shell_key = match regext::try_open_subkey_with_flags( &key, - regext::blank_path_guard(Self::NAMEOF_SHELL_VERB_PART1)?, + Self::NAMEOF_SHELL_VERB_PART1, PERM_R, )? { Some(key) => key, @@ -139,7 +137,7 @@ impl ApplicationsKey { // Get command subkey. let command_key = match regext::try_open_subkey_with_flags( &verb_key, - regext::blank_path_guard(Self::NAMEOF_SHELL_VERB_PART3)?, + Self::NAMEOF_SHELL_VERB_PART3, PERM_R, )? { Some(key) => key, @@ -167,24 +165,20 @@ impl ApplicationsKey { match sv { Some(sv) => { // Create shell subkey - let (shell_key, _) = key.create_subkey_with_flags( - regext::blank_path_guard(Self::NAMEOF_SHELL_VERB_PART1)?, - PERM_RW, - )?; + let (shell_key, _) = + key.create_subkey_with_flags(Self::NAMEOF_SHELL_VERB_PART1, PERM_RW)?; // Create verb key let (verb_key, _) = shell_key.create_subkey_with_flags(sv.get_verb().inner(), PERM_RW)?; // Create command key - let (command_key, _) = verb_key.create_subkey_with_flags( - regext::blank_path_guard(Self::NAMEOF_SHELL_VERB_PART3)?, - PERM_RW, - )?; + let (command_key, _) = + verb_key.create_subkey_with_flags(Self::NAMEOF_SHELL_VERB_PART3, PERM_RW)?; // Set command key default value command_key.set_value(Self::NAMEOF_SHELL_VERB_PART4, &sv.get_command().full())?; } None => { // Delete shell and its all subkey. - key.delete_subkey_all(regext::blank_path_guard(Self::NAMEOF_SHELL_VERB_PART1)?)?; + key.delete_subkey_all(Self::NAMEOF_SHELL_VERB_PART1)?; } } @@ -199,7 +193,7 @@ impl ApplicationsKey { // Get default icon subkey let default_icon_key = match regext::try_open_subkey_with_flags( &key, - regext::blank_path_guard(Self::NAMEOF_DEFAULT_ICON_PART1)?, + Self::NAMEOF_DEFAULT_ICON_PART1, PERM_R, )? { Some(key) => key, @@ -218,16 +212,14 @@ impl ApplicationsKey { match icon { Some(icon) => { // Create default icon subkey - let (default_icon_key, _) = key.create_subkey_with_flags( - regext::blank_path_guard(Self::NAMEOF_DEFAULT_ICON_PART1)?, - PERM_RW, - )?; + let (default_icon_key, _) = + key.create_subkey_with_flags(Self::NAMEOF_DEFAULT_ICON_PART1, PERM_RW)?; // Set default value of default icon subkey. default_icon_key.set_value(Self::NAMEOF_DEFAULT_ICON_PART2, &icon.to_string())?; } None => { // Delete shell and its all subkey. - key.delete_subkey_all(regext::blank_path_guard(Self::NAMEOF_DEFAULT_ICON_PART1)?)?; + key.delete_subkey_all(Self::NAMEOF_DEFAULT_ICON_PART1)?; } } @@ -270,14 +262,11 @@ impl ApplicationsKey { pub fn get_supported_types(&self, view: View) -> Result>> { let key = self.open_view_for_getter(view)?; // Get supported types subkey - let supported_types_key = match regext::try_open_subkey_with_flags( - &key, - regext::blank_path_guard(Self::NAMEOF_SUPPORTED_TYPES)?, - PERM_R, - )? { - Some(key) => key, - None => return Ok(None), - }; + let supported_types_key = + match regext::try_open_subkey_with_flags(&key, Self::NAMEOF_SUPPORTED_TYPES, PERM_R)? { + Some(key) => key, + None => return Ok(None), + }; // Fetch all sub-values let key_names = regext::get_all_string_subkey_names(&supported_types_key)?; // Map the result @@ -299,20 +288,19 @@ impl ApplicationsKey { match tys { Some(tys) => { // Create supported types key - let (supported_types_key, _) = key.create_subkey_with_flags( - regext::blank_path_guard(Self::NAMEOF_SUPPORTED_TYPES)?, - PERM_RW, - )?; + let (supported_types_key, _) = + key.create_subkey_with_flags(Self::NAMEOF_SUPPORTED_TYPES, PERM_RW)?; // Clean all contents of this key regext::clean_all_contents(&supported_types_key)?; // Add file types one by one for ty in tys { - supported_types_key.set_value(ty.dotted_inner(), &"")?; + supported_types_key + .set_value(regext::blank_path_guard(ty.dotted_inner())?, &"")?; } } None => { // Delete this subkey. - key.delete_subkey_all(regext::blank_path_guard(Self::NAMEOF_SUPPORTED_TYPES)?)?; + key.delete_subkey_all(Self::NAMEOF_SUPPORTED_TYPES)?; } } diff --git a/wfassoc/src/lowlevel/ext_key.rs b/wfassoc/src/lowlevel/ext_key.rs index d3585f1..31e1c5c 100644 --- a/wfassoc/src/lowlevel/ext_key.rs +++ b/wfassoc/src/lowlevel/ext_key.rs @@ -41,11 +41,9 @@ impl ExtKey { }; let classes = match territory { OpenKeyTerritory::User | OpenKeyTerritory::System => { - hk.open_subkey_with_flags(regext::blank_path_guard(Self::FULL_CLASSES)?, perms)? - } - OpenKeyTerritory::Hybrid => { - hk.open_subkey_with_flags(regext::blank_path_guard(Self::PARTIAL_CLASSES)?, perms)? + hk.open_subkey_with_flags(Self::FULL_CLASSES, perms)? } + OpenKeyTerritory::Hybrid => hk.open_subkey_with_flags(Self::PARTIAL_CLASSES, perms)?, }; // open extension key if possible let this_ext = regext::try_open_subkey_with_flags( @@ -141,14 +139,12 @@ impl ExtKey { pub fn get_open_with_progids(&self, view: View) -> Result>> { let key = self.open_view_for_getter(view)?; // Get OpenWithProgIds subkey - let open_with_progids_key = match regext::try_open_subkey_with_flags( - &key, - regext::blank_path_guard(Self::NAMEOF_OPEN_WITH_PROGIDS)?, - PERM_R, - )? { - Some(key) => key, - None => return Ok(None), - }; + let open_with_progids_key = + match regext::try_open_subkey_with_flags(&key, Self::NAMEOF_OPEN_WITH_PROGIDS, PERM_R)? + { + Some(key) => key, + None => return Ok(None), + }; // Fetch all sub-values let key_names = regext::get_all_string_subkey_names(&open_with_progids_key)?; // Map the result @@ -166,16 +162,18 @@ impl ExtKey { pub fn is_in_open_with_progids(&self, view: View, pid: &LosseProgId) -> Result { let key = self.open_view_for_getter(view)?; // Get OpenWithProgIds subkey - let open_with_progids_key = match regext::try_open_subkey_with_flags( - &key, - regext::blank_path_guard(Self::NAMEOF_OPEN_WITH_PROGIDS)?, - PERM_R, - )? { - Some(key) => key, - None => return Ok(false), - }; + let open_with_progids_key = + match regext::try_open_subkey_with_flags(&key, Self::NAMEOF_OPEN_WITH_PROGIDS, PERM_R)? + { + Some(key) => key, + None => return Ok(false), + }; // Check whether there is given ProgId - Ok(regext::try_get_value::(&open_with_progids_key, pid.to_string())?.is_some()) + Ok(regext::try_get_value::( + &open_with_progids_key, + regext::blank_path_guard(pid.to_string())?, + )? + .is_some()) } /// @@ -185,12 +183,10 @@ impl ExtKey { pub fn add_into_open_with_progids(&mut self, scope: Scope, pid: &LosseProgId) -> Result<()> { let key = self.open_scope_for_setter(scope)?; // Get subkey - let (open_with_progids_key, _) = key.create_subkey_with_flags( - regext::blank_path_guard(Self::NAMEOF_OPEN_WITH_PROGIDS)?, - PERM_RW, - )?; + let (open_with_progids_key, _) = + key.create_subkey_with_flags(Self::NAMEOF_OPEN_WITH_PROGIDS, PERM_RW)?; // Add value - open_with_progids_key.set_value(pid.to_string(), &"")?; + open_with_progids_key.set_value(regext::blank_path_guard(pid.to_string())?, &"")?; Ok(()) } @@ -202,7 +198,7 @@ impl ExtKey { // Try get subkey let open_with_progids_key = match regext::try_open_subkey_with_flags( &key, - regext::blank_path_guard(Self::NAMEOF_OPEN_WITH_PROGIDS)?, + Self::NAMEOF_OPEN_WITH_PROGIDS, PERM_RW, )? { Some(key) => key, diff --git a/wfassoc/src/lowlevel/progid_key.rs b/wfassoc/src/lowlevel/progid_key.rs index 5f954a4..10935b6 100644 --- a/wfassoc/src/lowlevel/progid_key.rs +++ b/wfassoc/src/lowlevel/progid_key.rs @@ -41,11 +41,9 @@ impl ProgIdKey { }; let classes = match territory { OpenKeyTerritory::User | OpenKeyTerritory::System => { - hk.open_subkey_with_flags(regext::blank_path_guard(Self::FULL_CLASSES)?, perms)? - } - OpenKeyTerritory::Hybrid => { - hk.open_subkey_with_flags(regext::blank_path_guard(Self::PARTIAL_CLASSES)?, perms)? + hk.open_subkey_with_flags(Self::FULL_CLASSES, perms)? } + OpenKeyTerritory::Hybrid => hk.open_subkey_with_flags(Self::PARTIAL_CLASSES, perms)?, }; // open ProgId key if possible let this_progid = regext::try_open_subkey_with_flags( @@ -148,7 +146,7 @@ impl ProgIdKey { // Get shell subkey let shell_key = match regext::try_open_subkey_with_flags( &key, - regext::blank_path_guard(Self::NAMEOF_SHELL_VERB_PART1)?, + Self::NAMEOF_SHELL_VERB_PART1, PERM_R, )? { Some(key) => key, @@ -170,7 +168,7 @@ impl ProgIdKey { // Get command subkey. let command_key = match regext::try_open_subkey_with_flags( &verb_key, - regext::blank_path_guard(Self::NAMEOF_SHELL_VERB_PART3)?, + Self::NAMEOF_SHELL_VERB_PART3, PERM_R, )? { Some(key) => key, @@ -198,24 +196,20 @@ impl ProgIdKey { match sv { Some(sv) => { // Create shell subkey - let (shell_key, _) = key.create_subkey_with_flags( - regext::blank_path_guard(Self::NAMEOF_SHELL_VERB_PART1)?, - PERM_RW, - )?; + let (shell_key, _) = + key.create_subkey_with_flags(Self::NAMEOF_SHELL_VERB_PART1, PERM_RW)?; // Create verb key let (verb_key, _) = shell_key.create_subkey_with_flags(sv.get_verb().inner(), PERM_RW)?; // Create command key - let (command_key, _) = verb_key.create_subkey_with_flags( - regext::blank_path_guard(Self::NAMEOF_SHELL_VERB_PART3)?, - PERM_RW, - )?; + let (command_key, _) = + verb_key.create_subkey_with_flags(Self::NAMEOF_SHELL_VERB_PART3, PERM_RW)?; // Set command key default value command_key.set_value(Self::NAMEOF_SHELL_VERB_PART4, &sv.get_command().full())?; } None => { // Delete shell and its all subkey. - key.delete_subkey_all(regext::blank_path_guard(Self::NAMEOF_SHELL_VERB_PART1)?)?; + key.delete_subkey_all(Self::NAMEOF_SHELL_VERB_PART1)?; } } @@ -264,7 +258,7 @@ impl ProgIdKey { // Get default icon subkey let default_icon_key = match regext::try_open_subkey_with_flags( &key, - regext::blank_path_guard(Self::NAMEOF_DEFAULT_ICON_PART1)?, + Self::NAMEOF_DEFAULT_ICON_PART1, PERM_R, )? { Some(key) => key, @@ -283,16 +277,14 @@ impl ProgIdKey { match icon { Some(icon) => { // Create default icon subkey - let (default_icon_key, _) = key.create_subkey_with_flags( - regext::blank_path_guard(Self::NAMEOF_DEFAULT_ICON_PART1)?, - PERM_RW, - )?; + let (default_icon_key, _) = + key.create_subkey_with_flags(Self::NAMEOF_DEFAULT_ICON_PART1, PERM_RW)?; // Set default value of default icon subkey. default_icon_key.set_value(Self::NAMEOF_DEFAULT_ICON_PART2, &icon.to_string())?; } None => { // Delete shell and its all subkey. - key.delete_subkey_all(regext::blank_path_guard(Self::NAMEOF_DEFAULT_ICON_PART1)?)?; + key.delete_subkey_all(Self::NAMEOF_DEFAULT_ICON_PART1)?; } } diff --git a/wfassoc/src/win32/regext.rs b/wfassoc/src/win32/regext.rs index fb14ed0..60d69e4 100644 --- a/wfassoc/src/win32/regext.rs +++ b/wfassoc/src/win32/regext.rs @@ -135,6 +135,9 @@ impl BlankPathError { /// Because it will cause unexpected behavior that returning key self, rather than subkey. /// This is VERY dangerous especially for those registry delete functions. /// So I create this function to prevent any harmful blank path was passed into registry function. +/// +/// This function MUST be used for the value, whose content can not be confirmed at compile time, +/// and it will be passed to get/set value, or create/delete key functions. pub fn blank_path_guard>(path: P) -> std::result::Result { if path.as_ref().is_empty() { Err(BlankPathError::new())