Skip to content

Commit 1688611

Browse files
committed
Replace read_to_string -> read_to_end to improve UTF-8 deserialization
See c1692e9 There are two more instances, encountered when debugging #148 > Instead of read_to_string(), use read_to_end() to read into a buffer, > then convert using String::from_utf8() and unwrap it. This gives a > better error message when UTF-8 fails to decode. which includes the offending bytes that can't be converted
1 parent 83c848f commit 1688611

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

protocol/src/protocol/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,9 @@ impl Serializable for String {
275275
impl Serializable for format::Component {
276276
fn read_from<R: io::Read>(buf: &mut R) -> Result<Self, Error> {
277277
let len = VarInt::read_from(buf)?.0;
278-
let mut ret = String::new();
279-
buf.take(len as u64).read_to_string(&mut ret)?;
278+
let mut bytes = Vec::<u8>::new();
279+
buf.take(len as u64).read_to_end(&mut bytes)?;
280+
let ret = String::from_utf8(bytes).unwrap();
280281
Result::Ok(Self::from_string(&ret[..]))
281282
}
282283
fn write_to<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {

0 commit comments

Comments
 (0)