Closed
Description
rustc crashes when compiling the following source code.
use std::env;
use std::process;
use std::str;
use std::io::{Error, Read};
extern crate hyper;
use hyper::client::Client;
use hyper::header::UserAgent;
use hyper::method::Method;
extern crate getopts;
use getopts::Options;
#[macro_use]
extern crate log;
extern crate env_logger;
extern crate xml;
use xml::attribute::OwnedAttribute;
use xml::reader::{EventReader, XmlEvent};
type IoError = std::io::Error;
type HyperError = hyper::error::Error;
type ParseError = xml::reader::Error;
const SPEEDTEST_CONFIG:&'static str = "https://www.speedtest.net/speedtest-config.php";
struct Config {
client: Vec<OwnedAttribute>,
times: Vec<OwnedAttribute>,
download: Vec<OwnedAttribute>,
upload: Vec<OwnedAttribute>
}
#[derive(Debug)]
enum SpeedtestError {
Http(HyperError),
Xml(ParseError),
}
impl From<ParseError> for SpeedtestError {
fn from(err: ParseError) -> SpeedtestError {
SpeedtestError::Xml(err)
}
}
impl From<HyperError> for SpeedtestError {
fn from(err: HyperError) -> SpeedtestError {
SpeedtestError::Http(err)
}
}
fn print_usage(program: &str, opts: Options) {
let brief = format!("Usage: {} [options]", program);
println!("{}", opts.usage(&brief));
}
fn find_xml_key<'r>(parser: &mut EventReader<&'r [u8]>, key: &str) -> Result<XmlEvent::StartElement, SpeedtestError> {
loop {
let evnt = parser.next();
match evnt {
Ok(XmlEvent::StartElement { ref name, .. }) if name.local_name == key => {
return Ok(evnt.unwrap());
},
Ok(XmlEvent::StartDocument { .. }) | Ok(XmlEvent::StartElement { .. }) | Ok(XmlEvent::EndElement { .. }) | Ok(XmlEvent::EndDocument) | Ok(XmlEvent::ProcessingInstruction { ..}) | Ok(XmlEvent::CData(_)) | Ok(XmlEvent::Comment(_)) | Ok(XmlEvent::Characters(_)) | Ok(XmlEvent::Whitespace(_)) => {
continue;
},
Err(e) => {
return Err(SpeedtestError::from(e));
}
}
}
}
fn find_xml_key_attrs<'r>(mut parser: EventReader<&'r [u8]>, key: &str) -> Result<Vec<OwnedAttribute>, SpeedtestError> {
match find_xml_key(&mut parser, key) {
Ok(XmlEvent::StartElement { name, attributes, .. }) => { return Ok(attributes); },
Err(e) => { return Err(e); }
}
}
fn get_config() -> Result<Config, SpeedtestError> {
//Gather config data from speedtest
let resp = try!(Client::new().request(Method::Get, SPEEDTEST_CONFIG).header(UserAgent("Mozilla/5.0".to_owned())).send());
info!("code={}; headers={};", resp.status, resp.headers);
let mut body = String::new();
resp.read_to_string(&mut body);
Ok(Config {
client: try!(find_xml_key_attrs(EventReader::from_str(&*body), "client")),
times: try!(find_xml_key_attrs(EventReader::from_str(&*body), "times")),
download: try!(find_xml_key_attrs(EventReader::from_str(&*body), "download")),
upload: try!(find_xml_key_attrs(EventReader::from_str(&*body), "download"))
})
}
fn main() {
env_logger::init().unwrap();
let args: Vec<String> = env::args().collect();
let program = args[0].clone();
let mut opts = Options::new();
opts.optflag("h", "help", "Print this help");
opts.optflag("l", "list", "Display a list of speedtest.net servers sorted by distance");
opts.optopt("s", "server", "Specify a server ID to test against", "SERVER");
opts.optopt("t", "timeout", "HTTP timeout in seconds. Default 10", "TIMEOUT");
let matches = match opts.parse(&args[1..]) {
Ok(m) => m,
Err(e) => { error!("{}", e.to_string()); process::exit(1); }
};
if matches.opt_present("h") {
print_usage(&program, opts);
process::exit(0);
}
let timeout: u16 = matches.opt_str("t").unwrap_or("10".to_string()).parse::<u16>().unwrap_or(10u16);
let server_id: u16 = matches.opt_str("s").unwrap_or("0".to_string()).parse::<u16>().unwrap_or(0u16);
info!("Timeout is {}", timeout);
info!("Server ID is {}", server_id);
get_config();
}
I expect this to compile or report error. Instead the compiler crashed.
`rustc --version --verbose`:
rustc 1.5.0 (3d7cd77e4 2015-12-04)
binary: rustc
commit-hash: 3d7cd77e442ce34eaac8a176ae8be17669498ebc
commit-date: 2015-12-04
host: x86_64-unknown-linux-gnu
release: 1.5.0
Backtrace:
thread 'rustc' panicked at 'called `Option::unwrap()` on a `None` value', ../src/libcore/option.rs:366
stack backtrace:
1: 0x7fa59f57fd70 - sys::backtrace::tracing::imp::write::h1fe79f3711caca0eUnt
2: 0x7fa59f586685 - panicking::log_panic::_<closure>::closure.39701
3: 0x7fa59f5860f5 - panicking::log_panic::hffc6d029fed602571nx
4: 0x7fa59f549663 - sys_common::unwind::begin_unwind_inner::h7045c1c64d9ab8edYgs
5: 0x7fa59f549fc8 - sys_common::unwind::begin_unwind_fmt::h40ee994bfe85f7a54fs
6: 0x7fa59f57dc31 - rust_begin_unwind
7: 0x7fa59f5d126f - panicking::panic_fmt::h4c8d12e3c05f3b8cZEK
8: 0x7fa59f5cb818 - panicking::panic::hb8a57f0c8013c90awDK
9: 0x7fa59e1eb8dc - astconv::finish_resolving_def_to_ty::hde6eb3c2566dd2a9XFw
10: 0x7fa59e1b58c5 - astconv::ast_ty_to_ty::hb9cc74773e37aba2rHw
11: 0x7fa59e1f6591 - astconv::ast_ty_arg_to_ty::h2acf49059af01d00Wyw
12: 0x7fa59e1f6202 - vec::_<impl>::from_iter::from_iter::h16226200009031787591
13: 0x7fa59e1f3da3 - astconv::convert_angle_bracketed_parameters::h4bb343d42f9c99b8sBv
14: 0x7fa59e1b2ef5 - astconv::ast_path_substs_for_ty::hecc70e907c9ec82brhv
15: 0x7fa59e1fdae8 - astconv::ast_path_to_ty::h91671619d7cd0b32k1v
16: 0x7fa59e1e9fa2 - astconv::finish_resolving_def_to_ty::hde6eb3c2566dd2a9XFw
17: 0x7fa59e1b58c5 - astconv::ast_ty_to_ty::hb9cc74773e37aba2rHw
18: 0x7fa59e1fa081 - astconv::convert_ty_with_lifetime_elision::h0f0ad7df906cca85xGv
19: 0x7fa59e202bba - astconv::ty_of_method_or_bare_fn::h6e52ea6931ff3e4cJZw
20: 0x7fa59e2254f9 - collect::type_scheme_of_item::h8f45bb014a0b1100Spz
21: 0x7fa59e21d744 - collect::convert_typed_item::h9b1d1867ccc71c5cGuz
22: 0x7fa59e2087b0 - collect::convert_item::hc32ae6512d69e4fa2sy
23: 0x7fa59e205337 - collect::collect_item_types::hc0eb478c58a079452Fx
24: 0x7fa59e24d82b - check_crate::h9d585667791002b9MrD
25: 0x7fa59fa548a9 - driver::phase_3_run_analysis_passes::_<closure>::closure.21889
26: 0x7fa59fa3a203 - middle::ty::context::_<impl>::create_and_enter::create_and_enter::h11311399452419292173
27: 0x7fa59fa351fe - driver::phase_3_run_analysis_passes::h9860921265290382560
28: 0x7fa59fa15c72 - driver::compile_input::h0fba4c102dd57a8f8ba
29: 0x7fa59fb6cabb - run_compiler::h9d54ca8784090b78sqc
30: 0x7fa59fb69b36 - sys_common::unwind::try::try_fn::try_fn::h11041572279465146525
31: 0x7fa59f57da98 - __rust_try
32: 0x7fa59f571aeb - sys_common::unwind::try::inner_try::h0c3b0e63f018cc29wds
33: 0x7fa59fb69e84 - boxed::_<impl>::call_box::call_box::h7023908653336004374
34: 0x7fa59f585153 - sys::thread::_<impl>::new::thread_start::h85eb4d682b5d5d4ffGw
35: 0x7fa599697609 - start_thread
36: 0x7fa59f20ca9c - __clone
37: 0x0 - <unknown>