Skip to content

Commit 42caf17

Browse files
committed
Fix panic when truncating non-ASCII channel keys and usernames
1 parent ab0f4af commit 42caf17

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

sable_network/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![feature(round_char_boundary)]
2+
13
pub mod prelude;
24

35
pub mod id;

sable_network/src/validated.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ impl Username {
127127
pub fn new_coerce(s: &str) -> Self {
128128
let mut s = s.to_string();
129129
s.retain(|c| c != '[');
130-
s.truncate(10);
130+
s.truncate(s.floor_char_boundary(10));
131131
// expect() is safe here; we've already truncated to the max length
132132
Self(ArrayString::try_from(s.as_str()).expect("Failed to convert string"))
133133
}
@@ -138,7 +138,7 @@ impl ChannelKey {
138138
let mut s = s.to_string();
139139
s.retain(|c| c > ' ' && c <= '~' && c != ':' && c != ',');
140140
let mut val = <Self as Validated>::Underlying::new();
141-
s.truncate(val.capacity());
141+
s.truncate(s.floor_char_boundary(val.capacity()));
142142
val.push_str(&s);
143143
Self::validate(&val).map(|()| Self(val))
144144
}

0 commit comments

Comments
 (0)