Skip to content

Commit

Permalink
Remove a panic and be more conservative about depth limit (#1160)
Browse files Browse the repository at this point in the history
It's conceivable someone else might have pushed a frame besides
`with_frame` so we should handle going _past_ the depth limit as well as
just hitting it.

Also remove an assert which, if wrong, would panic.
  • Loading branch information
graydon authored Nov 1, 2023
1 parent 0bbf4b6 commit 1ed5fe1
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions soroban-env-host/src/host/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use soroban_env_common::{
use crate::{
auth::AuthorizationManagerSnapshot,
budget::AsBudget,
err,
storage::{InstanceStorageMap, StorageMap},
xdr::{ContractExecutable, Hash, HostFunction, HostFunctionType, ScVal},
Error, Host, HostError, Object, Symbol, SymbolStr, TryFromVal, TryIntoVal, Val,
Expand Down Expand Up @@ -348,7 +349,7 @@ impl Host {
F: FnOnce() -> Result<Val, HostError>,
{
let start_depth = self.try_borrow_context()?.len();
if start_depth as u32 == DEFAULT_HOST_DEPTH_LIMIT {
if start_depth as u32 >= DEFAULT_HOST_DEPTH_LIMIT {
return Err(Error::from_type_and_code(
ScErrorType::Context,
ScErrorCode::ExceededLimit,
Expand All @@ -375,7 +376,15 @@ impl Host {
}
// Every push and pop should be matched; if not there is a bug.
let end_depth = self.try_borrow_context()?.len();
assert_eq!(start_depth, end_depth);
if start_depth != end_depth {
return Err(err!(
self,
(ScErrorType::Context, ScErrorCode::InternalError),
"frame-depth mismatch",
start_depth,
end_depth
));
}
res
}

Expand Down

0 comments on commit 1ed5fe1

Please sign in to comment.