Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 63e00dc

Browse files
authoredJun 26, 2024
Rollup merge of rust-lang#126980 - Borgerr:fix-extendfromslice-check, r=workingjubilee
set self.is_known_utf8 to false in extend_from_slice try-job: x86_64-msvc closes rust-lang#126977 Related to rust-lang#126885, rust-lang#126333, and [this conversation](<rust-lang@aa46a33#r143539097>)
2 parents aa47a8e + 5aac249 commit 63e00dc

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed
 

‎library/std/src/sys_common/wtf8.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ impl Wtf8Buf {
480480
#[inline]
481481
pub(crate) fn extend_from_slice(&mut self, other: &[u8]) {
482482
self.bytes.extend_from_slice(other);
483-
self.is_known_utf8 = self.is_known_utf8 || self.next_surrogate(0).is_none();
483+
self.is_known_utf8 = false;
484484
}
485485
}
486486

‎library/std/src/sys_common/wtf8/tests.rs

+24
Original file line numberDiff line numberDiff line change
@@ -725,3 +725,27 @@ fn wtf8_utf8_boundary_between_surrogates() {
725725
string.push(CodePoint::from_u32(0xD800).unwrap());
726726
check_utf8_boundary(&string, 3);
727727
}
728+
729+
#[test]
730+
fn wobbled_wtf8_plus_bytes_isnt_utf8() {
731+
let mut string: Wtf8Buf = unsafe { Wtf8::from_bytes_unchecked(b"\xED\xA0\x80").to_owned() };
732+
assert!(!string.is_known_utf8);
733+
string.extend_from_slice(b"some utf-8");
734+
assert!(!string.is_known_utf8);
735+
}
736+
737+
#[test]
738+
fn wobbled_wtf8_plus_str_isnt_utf8() {
739+
let mut string: Wtf8Buf = unsafe { Wtf8::from_bytes_unchecked(b"\xED\xA0\x80").to_owned() };
740+
assert!(!string.is_known_utf8);
741+
string.push_str("some utf-8");
742+
assert!(!string.is_known_utf8);
743+
}
744+
745+
#[test]
746+
fn unwobbly_wtf8_plus_utf8_is_utf8() {
747+
let mut string: Wtf8Buf = Wtf8Buf::from_str("hello world");
748+
assert!(string.is_known_utf8);
749+
string.push_str("some utf-8");
750+
assert!(string.is_known_utf8);
751+
}

0 commit comments

Comments
 (0)
Please sign in to comment.