From 9408f744c3ba8504c88e5e9b427ccf9173c35551 Mon Sep 17 00:00:00 2001 From: Joseph Perez Date: Tue, 23 Jul 2024 14:10:40 +0200 Subject: [PATCH] fix: fix canonization not updating `&mut str` length (#1254) --- commons/zenoh-keyexpr/src/key_expr/canon.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/commons/zenoh-keyexpr/src/key_expr/canon.rs b/commons/zenoh-keyexpr/src/key_expr/canon.rs index 8187467004..a8950b6d0c 100644 --- a/commons/zenoh-keyexpr/src/key_expr/canon.rs +++ b/commons/zenoh-keyexpr/src/key_expr/canon.rs @@ -98,6 +98,7 @@ impl Canonize for &mut str { let bytes = unsafe { self.as_bytes_mut() }; let length = canonize(bytes); bytes[length..].fill(b'\0'); + *self = &mut core::mem::take(self)[..length]; } } @@ -166,6 +167,8 @@ fn canonizer() { // &mut str remaining part is zeroed let mut s = String::from("$*$*$*/hello/$*$*/bye/$*$*"); - s.as_mut_str().canonize(); + let mut s_mut = s.as_mut_str(); + s_mut.canonize(); + assert_eq!(s_mut, "*/hello/*/bye/*"); assert_eq!(s, "*/hello/*/bye/*\0\0\0\0\0\0\0\0\0\0\0"); }