Skip to content

fix: prevent panic on no-op contract extend for non-existent entries#2600

Open
toller892 wants to merge 1 commit into
stellar:mainfrom
toller892:fix/contract-extend-noop-panic
Open

fix: prevent panic on no-op contract extend for non-existent entries#2600
toller892 wants to merge 1 commit into
stellar:mainfrom
toller892:fix/contract-extend-noop-panic

Conversation

@toller892
Copy link
Copy Markdown

Description

Fixes a panic (index out of bounds: the len is 0 but the index is 0) in stellar contract extend when the target entry does not exist.

Problem

When stellar contract extend is called against a non-existent contract ID, the transaction succeeds as a no-op — the changes array is empty. The code then calls client.get_full_ledger_entries(&keys) which returns an empty entries list for a non-existent entry. Accessing entry.entries[0] without checking for emptiness causes an index out of bounds panic at extend.rs:288.

Reproduction:

stellar contract extend --id CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABSC4 --ledgers-to-extend 1
# Transaction succeeds as no-op, then panics:
# thread 'main' panicked at .../extend.rs:288:42:
# index out of bounds: the len is 0 but the index is 0

Fix

  1. Primary fix: Check entry.entries.is_empty() before accessing entries[0] in the no-op path. Return Error::LedgerEntryNotFound if empty — consistent with how the same error is returned elsewhere in this function (lines 268, 277, 282).

  2. Defensive fix: Add a changes.len() < 2 guard before matching on changes[0] and changes[1], preventing a potential panic if the transaction meta is malformed (fewer than 2 change entries).

Both checks use the existing Error::LedgerEntryNotFound variant, which is already the error returned for all other "entry not found" cases in this function.

Fixes #2599

When  is called against a non-existent contract
ID, the transaction succeeds as a no-op (empty changes). The code then
tries to fetch the ledger entry and access  without
checking if the entries list is empty, causing an index-out-of-bounds
panic.

Also add a guard for the changes array having fewer than 2 elements
before matching on  and , preventing a
potential panic on malformed transaction meta.

Fixes stellar#2599
Copilot AI review requested due to automatic review settings June 2, 2026 11:58
@github-project-automation github-project-automation Bot moved this to Backlog (Not Ready) in DevX Jun 2, 2026
@chatgpt-codex-connector
Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Credits must be used to enable repository wide code reviews.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR hardens contract extend by adding guard checks to avoid indexing into empty RPC responses and to fail fast when expected ledger changes are missing.

Changes:

  • Return an error when get_full_ledger_entries returns no entries in the no-op path.
  • Return an error when changes contains fewer than two elements before indexing into changes[0] / changes[1].

Comment on lines +296 to +298
if changes.len() < 2 {
return Err(Error::LedgerEntryNotFound);
}
Comment on lines +288 to 291
if entry.entries.is_empty() {
return Err(Error::LedgerEntryNotFound);
}
let extension = entry.entries[0].live_until_ledger_seq.unwrap_or_default();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Backlog (Not Ready)

Development

Successfully merging this pull request may close these issues.

stellar contract extend panics with "index out of bounds" when the extension is a no-op

2 participants