remove null byte from string

This commit is contained in:
spuds 2023-01-11 13:02:44 +00:00
parent b268b8e8ac
commit 100c4a189f
No known key found for this signature in database
GPG key ID: 0B6CA6068E827C8F

View file

@ -38,7 +38,7 @@ impl<R: std::io::Read> ReadExt for R {
} }
fn read_string(&mut self) -> Result<String, crate::Error> { fn read_string(&mut self) -> Result<String, crate::Error> {
Ok(match self.read_i32::<LE>()? { let mut buf = match self.read_i32::<LE>()? {
size if size.is_negative() => { size if size.is_negative() => {
let mut buf = Vec::with_capacity(-size as usize); let mut buf = Vec::with_capacity(-size as usize);
for _ in 0..buf.capacity() { for _ in 0..buf.capacity() {
@ -47,7 +47,10 @@ impl<R: std::io::Read> ReadExt for R {
String::from_utf16(&buf)? String::from_utf16(&buf)?
} }
size => String::from_utf8(self.read_len(size as usize)?)?, size => String::from_utf8(self.read_len(size as usize)?)?,
}) };
// remove the null byte
buf.pop();
Ok(buf)
} }
fn read_len(&mut self, len: usize) -> Result<Vec<u8>, super::Error> { fn read_len(&mut self, len: usize) -> Result<Vec<u8>, super::Error> {