diff --git a/Makefile b/Makefile index 1039553a..7593cbdc 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ export RUSTFLAGS=-Dwarnings -Dclippy::all -Dclippy::pedantic CARGO_HACK_ARGS=--feature-powerset --exclude-features default --group-features base64,serde,arbitrary,hex -XDRGEN_VERSION=92e5c651 +XDRGEN_VERSION=b405294c CARGO_DOC_ARGS?=--open all: build test diff --git a/src/curr/generated.rs b/src/curr/generated.rs index cbc42fbf..fc532a94 100644 --- a/src/curr/generated.rs +++ b/src/curr/generated.rs @@ -1326,7 +1326,7 @@ impl ReadXdr for VecM { return Err(Error::LengthExceedsMax); } - let mut vec = Vec::with_capacity(len as usize); + let mut vec = Vec::new(); for _ in 0..len { let t = T::read_xdr(r)?; vec.push(t); diff --git a/src/next/generated.rs b/src/next/generated.rs index 6fde8785..998abefc 100644 --- a/src/next/generated.rs +++ b/src/next/generated.rs @@ -1326,7 +1326,7 @@ impl ReadXdr for VecM { return Err(Error::LengthExceedsMax); } - let mut vec = Vec::with_capacity(len as usize); + let mut vec = Vec::new(); for _ in 0..len { let t = T::read_xdr(r)?; vec.push(t); diff --git a/tests/vecm.rs b/tests/vecm.rs new file mode 100644 index 00000000..ae899af0 --- /dev/null +++ b/tests/vecm.rs @@ -0,0 +1,44 @@ +#![cfg(all( + any(feature = "curr", feature = "next"), + not(all(feature = "curr", feature = "next")) +))] +#![cfg(feature = "std")] + +#[cfg(feature = "curr")] +use stellar_xdr::curr as stellar_xdr; +#[cfg(feature = "next")] +use stellar_xdr::next as stellar_xdr; + +use stellar_xdr::{ReadXdr, ScVal}; + +#[test] +fn valid_len() { + let data = [ + 0x00, 0x00, 0x00, 0x11, // SCV_MAP + 0x00, 0x00, 0x00, 0x01, // Some + 0x00, 0x00, 0x00, 0x01, // map length + 0x00, 0x00, 0x00, 0x0f, // SCV_SYMBOL + 0x00, 0x00, 0x00, 0x03, // symbol length: 3 + 0x63, 0x6e, 0x74, 0x00, // symbol value : "cnt" + 0x00, 0x00, 0x00, 0x03, // SCV_U32 + 0x00, 0x00, 0x00, 0x2a, // 42 + ]; + let result = ScVal::from_xdr(data); + assert!(result.is_ok()); +} + +#[test] +fn invalid_len() { + let data = [ + 0x00, 0x00, 0x00, 0x11, // SCV_MAP + 0x00, 0x00, 0x00, 0x01, // Some + 0xff, 0xff, 0xff, 0xff, // map length (first byte invalid) + 0x00, 0x00, 0x00, 0x0f, // SCV_SYMBOL + 0x00, 0x00, 0x00, 0x03, // symbol length: 3 + 0x63, 0x6e, 0x74, 0x00, // symbol value : "cnt" + 0x00, 0x00, 0x00, 0x03, // SCV_U32 + 0x00, 0x00, 0x00, 0x2a, // 42 + ]; + let result = ScVal::from_xdr(data); + assert!(result.is_err()); +}