summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJustus Winter <justus@pep-project.org>2017-11-28 14:42:26 +0100
committerJustus Winter <justus@pep-project.org>2017-11-28 14:57:25 +0100
commitfd65c4be53ae30972281eb6f0aa483eed6fba6bd (patch)
treea3ab264c40c85fc0eae4942873007bbf6a84b03d /src
parent4399a10f0e5baada52a8019b12a4cfb818cef135 (diff)
Fix out-of-bounds access.
- Fix filling the stash when buffers of size one are passed to 'write'. - Add a test demonstrating the problem.
Diffstat (limited to 'src')
-rw-r--r--src/armor.rs29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/armor.rs b/src/armor.rs
index 39628caf..3042359a 100644
--- a/src/armor.rs
+++ b/src/armor.rs
@@ -181,6 +181,12 @@ impl<'a, W: Write> Write for Writer<'a, W> {
assert!(self.stash.len() < 3);
if self.stash.len() > 0 {
while self.stash.len() < 3 {
+ if input.len() == 0 {
+ /* We exhausted the input. Return now, any
+ * stashed bytes are encoded when finalizing the
+ * writer. */
+ return Ok(written);
+ }
self.stash.push(input[0]);
input = &input[1..];
written += 1;
@@ -544,6 +550,29 @@ mod test {
}
}
+ #[test]
+ fn enarmor_bytewise() {
+ for len in TEST_VECTORS.iter() {
+ let mut file = File::open(format!("tests/data/armor/test-{}.bin", len)).unwrap();
+ let mut bin = Vec::<u8>::new();
+ file.read_to_end(&mut bin).unwrap();
+
+ let mut file = File::open(format!("tests/data/armor/test-{}.asc", len)).unwrap();
+ let mut asc = Vec::<u8>::new();
+ file.read_to_end(&mut asc).unwrap();
+
+ let mut buf = Vec::new();
+ {
+ let mut w = Writer::new(&mut buf, Kind::File);
+ for (i, _) in bin.iter().enumerate() {
+ w.write(&bin[i..i+1]).unwrap();
+ }
+ }
+ assert_eq!(String::from_utf8_lossy(&buf),
+ String::from_utf8_lossy(&asc));
+ }
+ }
+
use super::Reader;
#[test]