about summary refs log tree commit diff
path: root/library/alloc/src/string.rs
diff options
context:
space:
mode:
authorGiacomo Stevanato <giaco.stevanato@gmail.com>2020-10-28 18:52:45 +0100
committerGiacomo Stevanato <giaco.stevanato@gmail.com>2020-10-29 11:58:12 +0100
commite83666f45e3d93439775daefda7800b2ab193d30 (patch)
tree0d92eab98542af5601b23b3572eb14404c57a863 /library/alloc/src/string.rs
parent1d6a0b0c7206448d22b17a0f8170b0d8e7c129f6 (diff)
downloadrust-e83666f45e3d93439775daefda7800b2ab193d30.tar.gz
rust-e83666f45e3d93439775daefda7800b2ab193d30.zip
Prevent String::retain from creating non-utf8 strings when abusing panic
Diffstat (limited to 'library/alloc/src/string.rs')
-rw-r--r--library/alloc/src/string.rs10
1 files changed, 6 insertions, 4 deletions
diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs
index 72ed036637d..ce216e5336e 100644
--- a/library/alloc/src/string.rs
+++ b/library/alloc/src/string.rs
@@ -1235,6 +1235,10 @@ impl String {
         let mut del_bytes = 0;
         let mut idx = 0;
 
+        unsafe {
+            self.vec.set_len(0);
+        }
+
         while idx < len {
             let ch = unsafe { self.get_unchecked(idx..len).chars().next().unwrap() };
             let ch_len = ch.len_utf8();
@@ -1255,10 +1259,8 @@ impl String {
             idx += ch_len;
         }
 
-        if del_bytes > 0 {
-            unsafe {
-                self.vec.set_len(len - del_bytes);
-            }
+        unsafe {
+            self.vec.set_len(len - del_bytes);
         }
     }