Skip to content

Ensure we strip NUL bytes before CString::new#632

Open
Shaptic wants to merge 1 commit intomainfrom
protecc-cstring
Open

Ensure we strip NUL bytes before CString::new#632
Shaptic wants to merge 1 commit intomainfrom
protecc-cstring

Conversation

@Shaptic
Copy link
Contributor

@Shaptic Shaptic commented Mar 17, 2026

What

Handle NulError from CString::new and strip \0 bytes.

Why

While our usage is safe because the strings come from the WASM host VM which never includes user-controlled messages and only uses printable ASCII characters, it's always better to code defensively.

Known limitations

n/a

Copilot AI review requested due to automatic review settings March 17, 2026 22:37
Copy link

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

This PR hardens Rust↔C FFI string handling by ensuring interior NUL (\0) bytes can’t cause CString::new() to panic, centralizing the behavior in a shared helper and updating call sites to use it.

Changes:

  • Added safe_cstring() helper in the ffi crate to strip interior NUL bytes on CString::new() failure.
  • Updated string_to_c() (and preflight error-string construction) to use safe_cstring() instead of CString::new(...).unwrap().
  • Removed a duplicated local string_to_c() helper from the preflight crate.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
cmd/stellar-rpc/lib/preflight/src/lib.rs Switches preflight error-string construction to safe_cstring() and removes the local string_to_c() helper.
cmd/stellar-rpc/lib/ffi/src/lib.rs Introduces safe_cstring() and routes string_to_c() through it to avoid panics on interior NUL bytes.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

fn default() -> Self {
Self {
error: CString::new(String::new()).unwrap().into_raw(),
error: safe_cstring(String::new()).into_raw(),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants