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 4f4533411e
commit f15ece0377
1 changed files with 3 additions and 2 deletions

View File

@ -304,7 +304,8 @@ pub fn write_string<W: io::Write>(buf: &mut W, s: &str) -> Result<(), protocol::
pub fn read_string<R: io::Read>(buf: &mut R) -> Result<String, protocol::Error> {
let len: i16 = buf.read_i16::<BigEndian>()?;
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(ret)
}