Skip to content

Commit

Permalink
Update xdrgen
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmcculloch committed Oct 28, 2023
1 parent 16f4d7c commit 02db205
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ CARGO_HACK_ARGS=--feature-powerset --exclude-features default --group-features b

CARGO_DOC_ARGS?=--open

XDRGEN_VERSION=64612a24
XDRGEN_VERSION=f334abd0
XDRGEN_TYPES_CUSTOM_STR_IMPL=PublicKey,AccountId,MuxedAccount,MuxedAccountMed25519,SignerKey,SignerKeyEd25519SignedPayload,NodeId,ScAddress

all: build test
Expand Down
36 changes: 30 additions & 6 deletions src/curr/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ use core::{array::TryFromSliceError, fmt, fmt::Debug, marker::Sized, ops::Deref,
#[cfg(feature = "std")]
use core::marker::PhantomData;

#[cfg(feature = "std")]
const MAX_PREALLOCATED_BYTES_READ: usize = 1024; // 1KB

// When feature alloc is turned off use static lifetime Box and Vec types.
#[cfg(not(feature = "alloc"))]
mod noalloc {
Expand Down Expand Up @@ -1287,8 +1290,15 @@ impl<const MAX: u32> ReadXdr for VecM<u8, MAX> {
return Err(Error::LengthExceedsMax);
}

let mut vec = vec![0u8; len as usize];
r.read_exact(&mut vec)?;
let mut vec = vec![0u8; 0];
let mut len_remaining = len as usize;
while len_remaining > 0 {
let len_read = core::cmp::min(len_remaining, MAX_PREALLOCATED_BYTES_READ);
let offset = vec.len();
vec.resize(vec.len() + len_read, 0);
r.read_exact(&mut vec[offset..])?;
len_remaining -= len_read;
}

let pad = &mut [0u8; 3][..pad_len(len as usize)];
r.read_exact(pad)?;
Expand Down Expand Up @@ -1685,8 +1695,15 @@ impl<const MAX: u32> ReadXdr for BytesM<MAX> {
return Err(Error::LengthExceedsMax);
}

let mut vec = vec![0u8; len as usize];
r.read_exact(&mut vec)?;
let mut vec = vec![0u8; 0];
let mut len_remaining = len as usize;
while len_remaining > 0 {
let len_read = core::cmp::min(len_remaining, MAX_PREALLOCATED_BYTES_READ);
let offset = vec.len();
vec.resize(vec.len() + len_read, 0);
r.read_exact(&mut vec[offset..])?;
len_remaining -= len_read;
}

let pad = &mut [0u8; 3][..pad_len(len as usize)];
r.read_exact(pad)?;
Expand Down Expand Up @@ -2068,8 +2085,15 @@ impl<const MAX: u32> ReadXdr for StringM<MAX> {
return Err(Error::LengthExceedsMax);
}

let mut vec = vec![0u8; len as usize];
r.read_exact(&mut vec)?;
let mut vec = vec![0u8; 0];
let mut len_remaining = len as usize;
while len_remaining > 0 {
let len_read = core::cmp::min(len_remaining, MAX_PREALLOCATED_BYTES_READ);
let offset = vec.len();
vec.resize(vec.len() + len_read, 0);
r.read_exact(&mut vec[offset..])?;
len_remaining -= len_read;
}

let pad = &mut [0u8; 3][..pad_len(len as usize)];
r.read_exact(pad)?;
Expand Down
36 changes: 30 additions & 6 deletions src/next/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ use core::{array::TryFromSliceError, fmt, fmt::Debug, marker::Sized, ops::Deref,
#[cfg(feature = "std")]
use core::marker::PhantomData;

#[cfg(feature = "std")]
const MAX_PREALLOCATED_BYTES_READ: usize = 1024; // 1KB

// When feature alloc is turned off use static lifetime Box and Vec types.
#[cfg(not(feature = "alloc"))]
mod noalloc {
Expand Down Expand Up @@ -1287,8 +1290,15 @@ impl<const MAX: u32> ReadXdr for VecM<u8, MAX> {
return Err(Error::LengthExceedsMax);
}

let mut vec = vec![0u8; len as usize];
r.read_exact(&mut vec)?;
let mut vec = vec![0u8; 0];
let mut len_remaining = len as usize;
while len_remaining > 0 {
let len_read = core::cmp::min(len_remaining, MAX_PREALLOCATED_BYTES_READ);
let offset = vec.len();
vec.resize(vec.len() + len_read, 0);
r.read_exact(&mut vec[offset..])?;
len_remaining -= len_read;
}

let pad = &mut [0u8; 3][..pad_len(len as usize)];
r.read_exact(pad)?;
Expand Down Expand Up @@ -1685,8 +1695,15 @@ impl<const MAX: u32> ReadXdr for BytesM<MAX> {
return Err(Error::LengthExceedsMax);
}

let mut vec = vec![0u8; len as usize];
r.read_exact(&mut vec)?;
let mut vec = vec![0u8; 0];
let mut len_remaining = len as usize;
while len_remaining > 0 {
let len_read = core::cmp::min(len_remaining, MAX_PREALLOCATED_BYTES_READ);
let offset = vec.len();
vec.resize(vec.len() + len_read, 0);
r.read_exact(&mut vec[offset..])?;
len_remaining -= len_read;
}

let pad = &mut [0u8; 3][..pad_len(len as usize)];
r.read_exact(pad)?;
Expand Down Expand Up @@ -2068,8 +2085,15 @@ impl<const MAX: u32> ReadXdr for StringM<MAX> {
return Err(Error::LengthExceedsMax);
}

let mut vec = vec![0u8; len as usize];
r.read_exact(&mut vec)?;
let mut vec = vec![0u8; 0];
let mut len_remaining = len as usize;
while len_remaining > 0 {
let len_read = core::cmp::min(len_remaining, MAX_PREALLOCATED_BYTES_READ);
let offset = vec.len();
vec.resize(vec.len() + len_read, 0);
r.read_exact(&mut vec[offset..])?;
len_remaining -= len_read;
}

let pad = &mut [0u8; 3][..pad_len(len as usize)];
r.read_exact(pad)?;
Expand Down
36 changes: 35 additions & 1 deletion tests/vecm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use stellar_xdr::curr as stellar_xdr;
#[cfg(feature = "next")]
use stellar_xdr::next as stellar_xdr;

use stellar_xdr::{ReadXdr, ScVal};
use stellar_xdr::{BytesM, ReadXdr, ScVal};

#[test]
fn valid_len() {
Expand Down Expand Up @@ -42,3 +42,37 @@ fn invalid_len() {
let result = ScVal::from_xdr(data);
assert!(result.is_err());
}

#[test]
fn valid_bytes_len() {
let data = [
0x00, 0x00, 0x00, 0x08, // length
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
];
let result: Result<BytesM, _> = BytesM::from_xdr(data);
assert_eq!(
result,
Ok(BytesM::try_from([1, 2, 3, 4, 5, 6, 7, 8]).unwrap())
);
}

#[test]
fn valid_bytes_len_greater_than_preallocated_bytes_limit() {
let mut data = [0x01u8; 3000];
data[0] = 0x00; // length
data[1] = 0x00; // length
data[2] = 0x0B; // length
data[3] = 0xB4; // length
let result: Result<BytesM, _> = BytesM::from_xdr(data);
assert_eq!(result, Ok(BytesM::try_from([0x01u8; 2996]).unwrap()));
}

#[test]
fn invalid_bytes_len() {
let data = [
0xFF, 0xFF, 0xFF, 0xFF, // length
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
];
let result: Result<BytesM, _> = BytesM::from_xdr(data);
assert!(result.is_err());
}

0 comments on commit 02db205

Please sign in to comment.