Fix utf-8 handling when reading malformed messages.

This commit is contained in:
Ty Overby 2015-01-07 10:18:39 -08:00
parent ee09eb19da
commit b4023f5281
3 changed files with 16 additions and 2 deletions

View File

@ -2,7 +2,6 @@
#![crate_type = "rlib"]
#![crate_type = "dylib"]
#![feature(old_orphan_check)]
#![feature(associated_types)]
extern crate "rustc-serialize" as rustc_serialize;

View File

@ -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> {

View File

@ -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());
}