Skip to content

Commit

Permalink
Fix string alloc (#28)
Browse files Browse the repository at this point in the history
* Fix non guarded alloc in string deserialization

* Bump version to 0.2.7
  • Loading branch information
ilblackdragon authored and maxzaver committed Oct 20, 2019
1 parent c5693fc commit d77b8a5
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 13 deletions.
12 changes: 6 additions & 6 deletions borsh-rs/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion borsh-rs/borsh-derive-internal/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "borsh-derive-internal"
version = "0.2.6"
version = "0.2.7"
authors = ["Near Inc <hello@nearprotocol.com>"]
edition = "2018"
license = "Apache-2.0"
Expand Down
4 changes: 2 additions & 2 deletions borsh-rs/borsh-derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "borsh-derive"
version = "0.2.6"
version = "0.2.7"
authors = ["Near Inc <hello@nearprotocol.com>"]
edition = "2018"
license = "Apache-2.0"
Expand All @@ -16,6 +16,6 @@ Binary Object Representation Serializer for Hashing
proc-macro = true

[dependencies]
borsh-derive-internal = { path = "../borsh-derive-internal" , version="0.2.6"}
borsh-derive-internal = { path = "../borsh-derive-internal" , version="0.2.7"}
syn = {version = "1", features = ["full", "fold"] }

4 changes: 2 additions & 2 deletions borsh-rs/borsh/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "borsh"
version = "0.2.6"
version = "0.2.7"
authors = ["Near Inc <hello@nearprotocol.com>"]
edition = "2018"
license = "Apache-2.0"
Expand All @@ -13,7 +13,7 @@ Binary Object Representation Serializer for Hashing
"""

[dependencies]
borsh-derive = { path = "../borsh-derive", version = "0.2.6" }
borsh-derive = { path = "../borsh-derive", version = "0.2.7" }

[features]
default = ["std"]
Expand Down
7 changes: 5 additions & 2 deletions borsh-rs/borsh/src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,11 @@ impl BorshDeserialize for String {
#[inline]
fn deserialize<R: Read>(reader: &mut R) -> Result<Self, Error> {
let len = u32::deserialize(reader)?;
let mut result = vec![0; len as usize];
reader.read_exact(&mut result)?;
// TODO(16): return capacity allocation when we have the size of the buffer left from the reader.
let mut result = Vec::new();
for _ in 0..len {
result.push(u8::deserialize(reader)?);
}
String::from_utf8(result)
.map_err(|err| std::io::Error::new(std::io::ErrorKind::InvalidData, err.to_string()))
}
Expand Down
6 changes: 6 additions & 0 deletions borsh-rs/borsh/tests/test_de_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ fn test_invalid_length() {
assert_eq!(<Vec<u64>>::try_from_slice(&bytes).unwrap_err().to_string(), "failed to fill whole buffer");
}

#[test]
fn test_invalid_length_string() {
let bytes = vec![255u8; 4];
assert_eq!(String::try_from_slice(&bytes).unwrap_err().to_string(), "failed to fill whole buffer");
}

#[test]
fn test_non_utf_string() {
let bytes = vec![1, 0, 0, 0, 0xC0];
Expand Down

0 comments on commit d77b8a5

Please sign in to comment.