Skip to content

Commit

Permalink
Change string_new_from_slice to use &[u8] instead of &str.
Browse files Browse the repository at this point in the history
`String` host type doesn't provide encoding guarantees and usually is encoded is `&[u8]`; this function is inconsistent with the rest of the API.
  • Loading branch information
dmkozh committed Dec 1, 2023
1 parent a31cc86 commit f770d8b
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 20 deletions.
2 changes: 1 addition & 1 deletion soroban-env-common/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ pub trait EnvBase: Sized + Clone {
fn bytes_new_from_slice(&self, slice: &[u8]) -> Result<BytesObject, Self::Error>;

/// Form a new `String` host object from a slice of client memory.
fn string_new_from_slice(&self, slice: &str) -> Result<StringObject, Self::Error>;
fn string_new_from_slice(&self, slice: &[u8]) -> Result<StringObject, Self::Error>;

/// Form a new `Symbol` host object from a slice of client memory.
fn symbol_new_from_slice(&self, slice: &str) -> Result<SymbolObject, Self::Error>;
Expand Down
3 changes: 2 additions & 1 deletion soroban-env-common/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ impl<E: Env> TryFromVal<E, &str> for StringObject {
type Error = crate::Error;
#[inline(always)]
fn try_from_val(env: &E, val: &&str) -> Result<StringObject, Self::Error> {
env.string_new_from_slice(val).map_err(Into::into)
env.string_new_from_slice(val.as_bytes())
.map_err(Into::into)
}
}

Expand Down
2 changes: 1 addition & 1 deletion soroban-env-guest/src/guest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl EnvBase for Guest {
self.bytes_new_from_linear_memory(lm_pos, len)
}

fn string_new_from_slice(&self, slice: &str) -> Result<StringObject, Self::Error> {
fn string_new_from_slice(&self, slice: &[u8]) -> Result<StringObject, Self::Error> {
sa::assert_eq_size!(u32, *const u8);
sa::assert_eq_size!(u32, usize);
let lm_pos: U32Val = Val::from_u32(slice.as_ptr() as u32);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{err, HostError};

use soroban_builtin_sdk_macros::contractimpl;
use soroban_env_common::xdr::Asset;
use soroban_env_common::{Compare, ConversionError, Env, EnvBase, TryFromVal, TryIntoVal};
use soroban_env_common::{Compare, Env, EnvBase, TryFromVal, TryIntoVal};

use super::admin::{read_administrator, write_administrator};
use super::asset_info::read_asset_info;
Expand Down Expand Up @@ -112,10 +112,7 @@ impl StellarAssetContract {
AssetInfo::AlphaNum4(AlphaNum4AssetInfo {
asset_code: String::try_from_val(
e,
&e.string_new_from_slice(
core::str::from_utf8(&asset4.asset_code.0)
.map_err(|_| ConversionError)?,
)?,
&e.string_new_from_slice(&asset4.asset_code.0)?,
)?,
issuer: BytesN::<32>::try_from_val(
e,
Expand All @@ -131,10 +128,7 @@ impl StellarAssetContract {
AssetInfo::AlphaNum12(AlphaNum12AssetInfo {
asset_code: String::try_from_val(
e,
&e.string_new_from_slice(
core::str::from_utf8(&asset12.asset_code.0)
.map_err(|_| ConversionError)?,
)?,
&e.string_new_from_slice(&asset12.asset_code.0)?,
)?,
issuer: BytesN::<32>::try_from_val(
e,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,15 @@ fn render_sep0011_asset<const N: usize>(
s.push(':');
s.push_str(&ed25519::PublicKey(issuer.to_array()?).to_string());
Ok((
String::try_from_val(e, &e.string_new_from_slice(s.as_str())?)?,
String::try_from_val(e, &e.string_new_from_slice(s.as_bytes())?)?,
symbol,
))
}

pub fn set_metadata(e: &Host) -> Result<(), HostError> {
let name_and_symbol: (String, String) = match read_asset_info(e)? {
AssetInfo::Native => {
let n = String::try_from_val(e, &e.string_new_from_slice("native")?)?;
let n = String::try_from_val(e, &e.string_new_from_slice(b"native")?)?;
(n.clone(), n)
}
AssetInfo::AlphaNum4(asset) => {
Expand Down
6 changes: 2 additions & 4 deletions soroban-env-host/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,11 +779,9 @@ impl EnvBase for Host {
res
}

fn string_new_from_slice(&self, s: &str) -> Result<StringObject, HostError> {
fn string_new_from_slice(&self, s: &[u8]) -> Result<StringObject, HostError> {
call_env_call_hook!(self, s.len());
let res = self.add_host_object(ScString(
self.metered_slice_to_vec(s.as_bytes())?.try_into()?,
));
let res = self.add_host_object(ScString(self.metered_slice_to_vec(s)?.try_into()?));
call_env_ret_hook!(self, res);
res
}
Expand Down
2 changes: 1 addition & 1 deletion soroban-env-host/src/host/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ impl DebugArg for xdr::Hash {

impl DebugArg for str {
fn debug_arg_maybe_expensive_or_fallible(host: &Host, arg: &Self) -> Result<Val, HostError> {
host.string_new_from_slice(arg).map(|s| s.into())
host.string_new_from_slice(arg.as_bytes()).map(|s| s.into())
}
}

Expand Down
2 changes: 1 addition & 1 deletion soroban-env-host/src/test/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn str_conversions() -> Result<(), HostError> {
obj = host.bytes_push(obj, (c as u32).into())?;
}
let ss = "abcdefghijklmnopqrstuvwxyz";
let so = host.string_new_from_slice(ss)?;
let so = host.string_new_from_slice(ss.as_bytes())?;
let val = so.to_val();
let s: String = val.try_into_val(&*host)?;
assert_eq!(s, ss);
Expand Down

0 comments on commit f770d8b

Please sign in to comment.