Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 65 additions & 8 deletions cmd/soroban-cli/src/commands/tx/xdr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,77 @@ impl<R: Read> SkipWhitespace<R> {

impl<R: Read> Read for SkipWhitespace<R> {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
let n = self.inner.read(buf)?;
loop {
let n = self.inner.read(buf)?;
if n == 0 {
return Ok(0);
}

let mut written = 0;
for read in 0..n {
if !buf[read].is_ascii_whitespace() {
buf[written] = buf[read];
written += 1;
}
}

let mut written = 0;
for read in 0..n {
if !buf[read].is_ascii_whitespace() {
buf[written] = buf[read];
written += 1;
if written > 0 {
return Ok(written);
}
}
}
}

#[cfg(test)]
mod tests {
use super::*;
use std::io::Cursor;

#[test]
fn skip_whitespace_preserves_content() {
let input = Cursor::new(b"helloworld");
let mut reader = SkipWhitespace::new(input);
let mut result = String::new();
reader.read_to_string(&mut result).unwrap();
assert_eq!(result, "helloworld");
}

#[test]
fn skip_whitespace_strips_all_whitespace_types() {
let input = Cursor::new(b"hello \t\n\r world");
let mut reader = SkipWhitespace::new(input);
let mut result = String::new();
reader.read_to_string(&mut result).unwrap();
assert_eq!(result, "helloworld");
}
Comment on lines +83 to +99
Copy link

Copilot AI Mar 23, 2026

Choose a reason for hiding this comment

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

The new unit tests don’t exercise the bug fixed by the loop: the failure only happens when the inner reader returns a chunk that contains only whitespace but more non-whitespace bytes arrive in a later read. Cursor typically reads the whole buffer in one call, so these tests would also pass with the previous (buggy) implementation. Consider adding a test with a chunked/step reader (e.g., returns b"AAAA", then b"\n\n", then b"BBBB") to ensure SkipWhitespace::read() does not signal EOF early, and/or an integration test in soroban-test that pipes multi-line base64 into stellar tx hash/tx sign/tx send.

Copilot generated this review using guidance from repository custom instructions.

#[test]
fn skip_whitespace_handles_only_whitespace() {
let input = Cursor::new(b"\n \t \r\n");
let mut reader = SkipWhitespace::new(input);
let mut result = String::new();
reader.read_to_string(&mut result).unwrap();
assert_eq!(result, "");
}

#[test]
fn skip_whitespace_handles_empty_input() {
let input = Cursor::new(b"");
let mut reader = SkipWhitespace::new(input);
let mut result = String::new();
reader.read_to_string(&mut result).unwrap();
assert_eq!(result, "");
}

Ok(written)
#[test]
fn skip_whitespace_handles_leading_trailing_whitespace() {
let input = Cursor::new(b"\n\nhello\n\n");
let mut reader = SkipWhitespace::new(input);
let mut result = String::new();
reader.read_to_string(&mut result).unwrap();
assert_eq!(result, "hello");
}
}
//

pub fn unwrap_envelope_v1(tx_env: TransactionEnvelope) -> Result<Transaction, Error> {
let TransactionEnvelope::Tx(TransactionV1Envelope { tx, .. }) = tx_env else {
Expand Down
Loading