Replace read_to_string -> read_to_end to improve UTF-8 deserialization

See c1692e950a
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
This commit is contained in:
ice_iix 2019-05-12 14:08:53 -07:00
parent 83c848fa6f
commit 16886110be
1 changed files with 3 additions and 2 deletions

View File

@ -275,8 +275,9 @@ impl Serializable for String {
impl Serializable for format::Component {
fn read_from<R: io::Read>(buf: &mut R) -> Result<Self, Error> {
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::<u8>::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<W: io::Write>(&self, buf: &mut W) -> Result<(), Error> {