Repository: pyfisch/cbor (rust)

Issue:
deserializing big byte arrays using `from_reader` does not round-trip
```rust
extern crate serde_cbor;
extern crate serde_bytes;

use std::io::Cursor;
use serde_bytes::ByteBuf;

fn main() {
    let input = ByteBuf::from(vec![0u8; 2048 * 1024]);
    let cbor = serde_cbor::to_vec(&input).unwrap();
    let output: ByteBuf = serde_cbor::from_reader(Cursor::new(cbor.as_slice())).unwrap();
    assert_eq!(input.len(), output.len());
}
```

I would expect `input` and `output` to have the same length, but instead `output` is only 16KiB long.
It works fine though using `serde_cbor::from_slice` instead of `serde_cbor::from_reader`.

Pull request description:
Fix deserialization of large byte buffers
Closes #70

Patch:
diff --git a/src/read.rs b/src/read.rs
index 5e457e2..58d17d2 100644
--- a/src/read.rs
+++ b/src/read.rs
@@ -127,6 +127,7 @@ where
             }
 
             self.read_into(&mut scratch[scratch_offset..])?;
+            scratch_offset = scratch.len();
         }
 
         Ok(Reference::Copied)


Test patch:
diff --git a/tests/de.rs b/tests/de.rs
index 9a4c9b8..5224403 100644
--- a/tests/de.rs
+++ b/tests/de.rs
@@ -1,8 +1,10 @@
 extern crate serde_cbor;
+extern crate serde_bytes;
 
+use serde_bytes::ByteBuf;
 use std::collections::BTreeMap;
 
-use serde_cbor::{to_vec, Value, ObjectKey, error, de};
+use serde_cbor::{to_vec, Value, ObjectKey, error, de, from_reader};
 
 #[test]
 fn test_string1() {
@@ -213,3 +215,13 @@ fn test_object_determinism_roundtrip() {
         assert_eq!(&to_vec(&de::from_slice::<Value>(expected).unwrap()).unwrap(), expected);
     }
 }
+
+#[test]
+fn test_large_bytes() {
+    let expected = (0..2 * 1024 * 1024).map(|i| (i * 7) as u8).collect::<Vec<_>>();
+    let expected = ByteBuf::from(expected);
+    let v = to_vec(&expected).unwrap();
+
+    let actual = from_reader(&v[..]).unwrap();
+    assert_eq!(expected, actual);
+}