From 5c47ab11d609522ceedc4449386c1b1631a4a03c Mon Sep 17 00:00:00 2001 From: Oussama Teffahi Date: Tue, 3 Sep 2024 16:58:35 +0200 Subject: [PATCH 1/3] Handle case where keyexpr equals the prefix to strip --- plugins/zenoh-plugin-storage-manager/src/lib.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/plugins/zenoh-plugin-storage-manager/src/lib.rs b/plugins/zenoh-plugin-storage-manager/src/lib.rs index ac778f3633..9d7de01aec 100644 --- a/plugins/zenoh-plugin-storage-manager/src/lib.rs +++ b/plugins/zenoh-plugin-storage-manager/src/lib.rs @@ -443,12 +443,15 @@ pub fn strip_prefix( ); } - match key_expr.strip_prefix(prefix).as_slice() { - [stripped_key_expr] => { - if stripped_key_expr.is_empty() { - return Ok(None); - } + // `key_expr.strip_prefix` returns empty vec if `key_expr == prefix`, + // but also returns empty vec if `prefix` is not a prefix to `key_expr`. + // First case needs to be handled before calling `key_expr.strip_prefix` + if key_expr.as_str().eq(prefix.as_str()) { + return Ok(None); + } + match key_expr.strip_prefix(prefix).as_slice() { + [stripped_key_expr] if !stripped_key_expr.is_empty() => { OwnedKeyExpr::from_str(stripped_key_expr).map(Some) } _ => bail!("Failed to strip prefix < {} > from: {}", prefix, key_expr), From fefb7d6648455f031150a2c21c44d391bbe7939f Mon Sep 17 00:00:00 2001 From: Oussama Teffahi Date: Tue, 3 Sep 2024 17:38:33 +0200 Subject: [PATCH 2/3] Add comment --- plugins/zenoh-plugin-storage-manager/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/zenoh-plugin-storage-manager/src/lib.rs b/plugins/zenoh-plugin-storage-manager/src/lib.rs index 9d7de01aec..5597c9ccf7 100644 --- a/plugins/zenoh-plugin-storage-manager/src/lib.rs +++ b/plugins/zenoh-plugin-storage-manager/src/lib.rs @@ -451,6 +451,7 @@ pub fn strip_prefix( } match key_expr.strip_prefix(prefix).as_slice() { + // NOTE: `stripped_key_expr.is_empty()` should be impossible as "" is not a valid key expression [stripped_key_expr] if !stripped_key_expr.is_empty() => { OwnedKeyExpr::from_str(stripped_key_expr).map(Some) } From 0ec13b91974e5995eae662df6fd57915cbf54ef8 Mon Sep 17 00:00:00 2001 From: Oussama Teffahi Date: Tue, 3 Sep 2024 17:40:50 +0200 Subject: [PATCH 3/3] Remove unnecessary empty keyexpr check --- plugins/zenoh-plugin-storage-manager/src/lib.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/plugins/zenoh-plugin-storage-manager/src/lib.rs b/plugins/zenoh-plugin-storage-manager/src/lib.rs index 5597c9ccf7..77e53cc80d 100644 --- a/plugins/zenoh-plugin-storage-manager/src/lib.rs +++ b/plugins/zenoh-plugin-storage-manager/src/lib.rs @@ -452,9 +452,7 @@ pub fn strip_prefix( match key_expr.strip_prefix(prefix).as_slice() { // NOTE: `stripped_key_expr.is_empty()` should be impossible as "" is not a valid key expression - [stripped_key_expr] if !stripped_key_expr.is_empty() => { - OwnedKeyExpr::from_str(stripped_key_expr).map(Some) - } + [stripped_key_expr] => OwnedKeyExpr::from_str(stripped_key_expr).map(Some), _ => bail!("Failed to strip prefix < {} > from: {}", prefix, key_expr), } }