forked from M-Labs/artiq
1
0
Fork 0

runtime: remove unnecessary null bytes from session protocol.

This commit is contained in:
whitequark 2016-10-06 15:38:33 +00:00
parent 5428a866b3
commit bcdbd00e7b
2 changed files with 4 additions and 8 deletions

View File

@ -166,7 +166,7 @@ class CommGeneric:
return self._read_chunk(self._read_int32())
def _read_string(self):
return self._read_bytes()[:-1].decode("utf-8")
return self._read_bytes().decode("utf-8")
#
# Writer interface
@ -214,7 +214,7 @@ class CommGeneric:
self._write_buffer.append(value)
def _write_string(self, value):
self._write_bytes(value.encode("utf-8") + b"\0")
self._write_bytes(value.encode("utf-8"))
#
# Exported APIs

View File

@ -66,14 +66,10 @@ pub fn write_bytes(writer: &mut Write, value: &[u8]) -> io::Result<()> {
}
pub fn read_string(reader: &mut Read) -> io::Result<String> {
let mut bytes = try!(read_bytes(reader));
let len = bytes.len() - 1; // length without trailing \0
bytes.resize(len, 0); // FIXME: don't send \0 in the first place
let bytes = try!(read_bytes(reader));
String::from_utf8(bytes).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
}
pub fn write_string(writer: &mut Write, value: &str) -> io::Result<()> {
try!(write_u32(writer, (value.len() + 1) as u32));
try!(writer.write_all(value.as_bytes()));
write_u8(writer, 0)
write_bytes(writer, value.as_bytes())
}