From f15ece0377f4922cc726541850b109d10f1e8ad9 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 --- protocol/src/nbt/mod.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/protocol/src/nbt/mod.rs b/protocol/src/nbt/mod.rs index e72fad6..37c3eca 100644 --- a/protocol/src/nbt/mod.rs +++ b/protocol/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) }