Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct comment on String::copy_into_slice #1055

Merged
merged 3 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion soroban-sdk/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ impl Address {
pub(crate) fn contract_id(&self) -> BytesN<32> {
match self.try_contract_id() {
Some(contract_id) => contract_id,
None => panic!("Address is not a Contract ID"),
None => sdk_panic!("Address is not a Contract ID"),
}
}

Expand Down
31 changes: 25 additions & 6 deletions soroban-sdk/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,11 +561,15 @@ impl Bytes {

/// Copy the bytes into the given slice.
///
/// The minimum number of bytes are copied to either exhaust [Bytes] or fill
/// slice.
/// ### Panics
///
/// If the output slice and bytes are of different lengths.
#[inline(always)]
pub fn copy_into_slice(&self, slice: &mut [u8]) {
let env = self.env();
if self.len() as usize != slice.len() {
sdk_panic!("Bytes::copy_into_slice with mismatched slice length")
}
env.bytes_copy_to_slice(self.to_object(), Val::U32_ZERO, slice)
.unwrap_optimized();
}
Expand Down Expand Up @@ -1013,11 +1017,8 @@ impl<const N: usize> BytesN<N> {
}

/// Copy the bytes into the given slice.
///
/// The minimum number of bytes are copied to either exhaust [BytesN] or fill
/// slice.
#[inline(always)]
pub fn copy_into_slice(&self, slice: &mut [u8]) {
pub fn copy_into_slice(&self, slice: &mut [u8; N]) {
let env = self.env();
env.bytes_copy_to_slice(self.to_object(), Val::U32_ZERO, slice)
.unwrap_optimized();
Expand Down Expand Up @@ -1127,6 +1128,24 @@ mod test {
assert_eq!([1, 2, 3, 4], out);
}

#[test]
#[should_panic]
fn bytes_to_short_slice() {
let env = Env::default();
let b = Bytes::from_slice(&env, &[1, 2, 3, 4]);
let mut out = [0u8; 3];
b.copy_into_slice(&mut out);
}

#[test]
#[should_panic]
fn bytes_to_long_slice() {
let env = Env::default();
let b = Bytes::from_slice(&env, &[1, 2, 3, 4]);
let mut out = [0u8; 5];
b.copy_into_slice(&mut out);
}

#[test]
fn macro_bytes() {
let env = Env::default();
Expand Down
21 changes: 21 additions & 0 deletions soroban-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,27 @@ macro_rules! panic_error {
}};
}

/// An internal panic! variant that avoids including the string
/// when building for wasm (since it's just pointless baggage).
#[cfg(target_family = "wasm")]
macro_rules! sdk_panic {
($_msg:literal) => {
panic!()
};
() => {
panic!()
};
}
#[cfg(not(target_family = "wasm"))]
macro_rules! sdk_panic {
($msg:literal) => {
panic!($msg)
};
() => {
panic!()
};
}

/// Assert a condition and panic with the given error if it is false.
///
/// The first argument in the list must be a reference to an [Env].
Expand Down
28 changes: 26 additions & 2 deletions soroban-sdk/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,15 @@ impl String {

/// Copy the bytes in [String] into the given slice.
///
/// The minimum number of bytes are copied to either exhaust [String] or fill
/// slice.
/// ### Panics
///
/// If the output slice and string are of different lengths.
#[inline(always)]
pub fn copy_into_slice(&self, slice: &mut [u8]) {
let env = self.env();
if self.len() as usize != slice.len() {
sdk_panic!("String::copy_into_slice with mismatched slice length")
}
env.string_copy_to_slice(self.to_object(), Val::U32_ZERO, slice)
.unwrap_optimized();
}
Expand All @@ -235,4 +239,24 @@ mod test {
s.copy_into_slice(&mut out);
assert_eq!(msg.as_bytes(), out)
}

#[test]
#[should_panic]
fn string_to_short_slice() {
let env = Env::default();
let msg = "a message";
let s = String::from_slice(&env, msg);
let mut out = [0u8; 8];
s.copy_into_slice(&mut out);
}

#[test]
#[should_panic]
fn string_to_long_slice() {
let env = Env::default();
let msg = "a message";
let s = String::from_slice(&env, msg);
let mut out = [0u8; 10];
s.copy_into_slice(&mut out);
}
}