mirror of https://git.sr.ht/~stygianentity/bincode
Fix utf-8 handling when reading malformed messages.
This commit is contained in:
parent
ee09eb19da
commit
b4023f5281
|
|
@ -2,7 +2,6 @@
|
|||
#![crate_type = "rlib"]
|
||||
#![crate_type = "dylib"]
|
||||
#![feature(old_orphan_check)]
|
||||
#![feature(associated_types)]
|
||||
|
||||
extern crate "rustc-serialize" as rustc_serialize;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
use std::io::{Buffer, Reader, IoError, IoResult, OtherIoError};
|
||||
use std::error::Error;
|
||||
|
||||
use rustc_serialize::Decoder;
|
||||
|
||||
|
|
@ -72,7 +73,11 @@ impl<'a, R: Reader+Buffer> Decoder for DecoderReader<'a, R> {
|
|||
fn read_str(&mut self) -> IoResult<String> {
|
||||
let len = try!(self.read_uint());
|
||||
let vector = try!(self.reader.read_exact(len));
|
||||
Ok(String::from_utf8(vector).unwrap())
|
||||
String::from_utf8(vector).map_err(|e| IoError {
|
||||
kind: OtherIoError,
|
||||
desc: "invalid utf-8",
|
||||
detail: e.detail()
|
||||
})
|
||||
}
|
||||
fn read_enum<T, F>(&mut self, _: &str, f: F) -> IoResult<T> where
|
||||
F: FnOnce(&mut DecoderReader<'a, R>) -> IoResult<T> {
|
||||
|
|
|
|||
10
src/test.rs
10
src/test.rs
|
|
@ -165,3 +165,13 @@ fn unicode() {
|
|||
the_same("å".to_string());
|
||||
the_same("aåååååååa".to_string());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bad_unicode() {
|
||||
// This is a malformed message that contains bad utf8.
|
||||
// The decoding should return Err but not panic.
|
||||
let encoded = vec![0,0,0,0, 0,0,0,2, 97, 195];
|
||||
let decoded: Result<String, _> = decode(encoded, Infinite);
|
||||
|
||||
assert!(decoded.is_err());
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue