From 10e5d6f44186c6acdf87666a6f00744d20744d0c Mon Sep 17 00:00:00 2001 From: ice_iix Date: Sun, 12 May 2019 14:08:53 -0700 Subject: [PATCH] Replace read_to_string -> read_to_end to improve UTF-8 deserialization See https://github.com/iceiix/stevenarella/commit/c1692e950aceccb9872478ed43f65848d90dd347 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 --- src/nbt/mod.rs | 5 +++-- src/protocol/mod.rs | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/nbt/mod.rs b/src/nbt/mod.rs index e72fad6..37c3eca 100644 --- a/src/nbt/mod.rs +++ b/src/nbt/mod.rs @@ -304,7 +304,8 @@ pub fn write_string(buf: &mut W, s: &str) -> Result<(), protocol:: pub fn read_string(buf: &mut R) -> Result { let len: i16 = buf.read_i16::()?; - let mut ret = String::new(); - buf.take(len as u64).read_to_string(&mut ret)?; + let mut bytes = Vec::::new(); + buf.take(len as u64).read_to_end(&mut bytes)?; + let ret = String::from_utf8(bytes).unwrap(); Result::Ok(ret) } diff --git a/src/protocol/mod.rs b/src/protocol/mod.rs index da88f15..3f1ee80 100644 --- a/src/protocol/mod.rs +++ b/src/protocol/mod.rs @@ -275,8 +275,9 @@ impl Serializable for String { impl Serializable for format::Component { fn read_from(buf: &mut R) -> Result { let len = VarInt::read_from(buf)?.0; - let mut ret = String::new(); - buf.take(len as u64).read_to_string(&mut ret)?; + let mut bytes = Vec::::new(); + buf.take(len as u64).read_to_end(&mut bytes)?; + let ret = String::from_utf8(bytes).unwrap(); Result::Ok(Self::from_string(&ret[..])) } fn write_to(&self, buf: &mut W) -> Result<(), Error> {