Skip to content

Commit

Permalink
Don't allow invalid chars in Symbol objects. (#765)
Browse files Browse the repository at this point in the history
Co-authored-by: Graydon Hoare <graydon@pobox.com>
  • Loading branch information
brson and graydon authored Apr 18, 2023
1 parent b216dfc commit 1e3aba3
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 6 deletions.
28 changes: 22 additions & 6 deletions soroban-env-common/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,25 @@ impl<const N: u32> TryFrom<&StringM<N>> for SymbolSmall {
}

impl SymbolSmall {
#[doc(hidden)]
pub const fn validate_char(ch: char) -> Result<(), SymbolError> {
match SymbolSmall::encode_char(ch) {
Ok(_) => Ok(()),
Err(e) => Err(e),
}
}

const fn encode_char(ch: char) -> Result<u64, SymbolError> {
let v = match ch {
'_' => 1,
'0'..='9' => 2 + ((ch as u64) - ('0' as u64)),
'A'..='Z' => 12 + ((ch as u64) - ('A' as u64)),
'a'..='z' => 38 + ((ch as u64) - ('a' as u64)),
_ => return Err(SymbolError::BadChar(ch)),
};
Ok(v)
}

pub const fn try_from_bytes(b: &[u8]) -> Result<SymbolSmall, SymbolError> {
let mut n = 0;
let mut accum: u64 = 0;
Expand All @@ -167,12 +186,9 @@ impl SymbolSmall {
}
n += 1;
accum <<= CODE_BITS;
let v = match ch {
'_' => 1,
'0'..='9' => 2 + ((ch as u64) - ('0' as u64)),
'A'..='Z' => 12 + ((ch as u64) - ('A' as u64)),
'a'..='z' => 38 + ((ch as u64) - ('a' as u64)),
_ => return Err(SymbolError::BadChar(ch)),
let v = match SymbolSmall::encode_char(ch) {
Ok(v) => v,
Err(e) => return Err(e),
};
accum |= v;
}
Expand Down
3 changes: 3 additions & 0 deletions soroban-env-host/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1603,6 +1603,9 @@ impl EnvBase for Host {
}

fn symbol_new_from_slice(&self, s: &str) -> Result<SymbolObject, HostError> {
for ch in s.chars() {
SymbolSmall::validate_char(ch)?;
}
self.add_host_object(ScSymbol(s.as_bytes().to_vec().try_into()?))
}

Expand Down
1 change: 1 addition & 0 deletions soroban-env-host/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod num;
#[cfg(all(feature = "testutils", feature = "vm"))]
mod storage;
mod str;
mod symbol;
mod vec;

#[cfg(feature = "vm")]
Expand Down
50 changes: 50 additions & 0 deletions soroban-env-host/src/test/symbol.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use crate::{Host, HostError};
use soroban_env_common::{Symbol, TryFromVal};

#[test]
fn invalid_chars() -> Result<(), HostError> {
let host = Host::default();

let s = "#";
let s = Symbol::try_from_val(&host, &s);

assert!(s.is_err());

Ok(())
}

#[test]
fn overlong() -> Result<(), HostError> {
let host = Host::default();

let s = "123456789012345678901234567890___";
let s = Symbol::try_from_val(&host, &s);

assert!(s.is_err());

Ok(())
}

#[test]
fn max_len() -> Result<(), HostError> {
let host = Host::default();

let s = "123456789012345678901234567890__";
let s = Symbol::try_from_val(&host, &s);

assert!(s.is_ok());

Ok(())
}

#[test]
fn zero_len() -> Result<(), HostError> {
let host = Host::default();

let s = "";
let s = Symbol::try_from_val(&host, &s);

assert!(s.is_ok());

Ok(())
}

0 comments on commit 1e3aba3

Please # to comment.