Added hash verification to external source loading.

This commit is contained in:
Inokentiy Babushkin 2017-06-11 13:48:54 +02:00
parent a5b8851e22
commit 9a8bbe9da9
No known key found for this signature in database
GPG Key ID: 7EFC8EC5224DE8EC
2 changed files with 22 additions and 7 deletions

View File

@ -567,13 +567,8 @@ impl CodeMapper for CodeMap {
};
if *file_map.external_src.borrow() == ExternalSource::AbsentOk {
let mut external_src = file_map.external_src.borrow_mut();
if let Ok(src) = self.file_loader.read_file(Path::new(&filename)) {
*external_src = ExternalSource::Present(src);
return true;
} else {
*external_src = ExternalSource::AbsentErr;
}
let src = self.file_loader.read_file(Path::new(&filename)).ok();
return file_map.add_external_src(src);
}
false

View File

@ -604,6 +604,26 @@ impl FileMap {
lines.push(pos);
}
/// add externally loaded source.
/// if the hash of the input doesn't match or no input is supplied via None,
/// it is interpreted as an error and the corresponding enum variant is set.
pub fn add_external_src(&self, src: Option<String>) -> bool {
let mut external_src = self.external_src.borrow_mut();
if let Some(src) = src {
let mut hasher: StableHasher<u128> = StableHasher::new();
hasher.write(src.as_bytes());
if hasher.finish() == self.src_hash {
*external_src = ExternalSource::Present(src);
return true;
}
} else {
*external_src = ExternalSource::AbsentErr;
}
false
}
/// get a line from the list of pre-computed line-beginnings.
/// line-number here is 0-based.
pub fn get_line(&self, line_number: usize) -> Option<Cow<str>> {