s2-cli icon indicating copy to clipboard operation
s2-cli copied to clipboard

[Detail Bug] Raw formatter corrupts and expands non‑UTF‑8 data in write_record

Open detail-app[bot] opened this issue 10 months ago • 1 comments

Detail Bug Report

https://app.detail.dev/org_89d327b3-b883-4365-b6a3-46b6701342a9/bugs/bug_6d2ef23a-526d-4995-a6db-87986c75d2a7

Summary

  • Context: The RawBodyFormatter is used to write record bodies when reading from S2 streams, outputting the body content directly to stdout or a file.
  • Bug: The write_record method corrupts binary data by expanding invalid UTF-8 bytes, where each invalid byte becomes 3 bytes (the UTF-8 encoding of the replacement character U+FFFD).
  • Actual vs. expected: Instead of writing the original bytes from record.body, the code converts through a lossy UTF-8 string and back to bytes, which replaces each invalid UTF-8 byte with [239, 191, 189] (the UTF-8 encoding of �). Expected behavior would be to either write the original bytes unchanged or document that output size can be significantly larger than input.
  • Impact: Users reading binary data from S2 streams with the default format will receive corrupted output that is both incorrect and larger than the original data, making the output unusable for binary processing or re-import.

Code with bug

impl RecordWriter for RawFormatter {
    async fn write_record(
        record: &SequencedRecord,
        writer: &mut (impl AsyncWrite + Unpin),
    ) -> io::Result<()> {
        let s = String::from_utf8_lossy(&record.body);  // <-- BUG 🔴 Lossy conversion replaces invalid bytes with U+FFFD
        writer.write_all(s.as_bytes()).await
    }
}

Example

  • Input body bytes: [0xFF, 0xFE, 0xFD] (invalid UTF-8)
  • String::from_utf8_lossy(...) → "���"
  • "���".as_bytes() → [239, 191, 189, 239, 191, 189, 239, 191, 189] (9 bytes)

Result: Original 3 bytes become 9 bytes; output is different and larger, confirming corruption and expansion.

Recommended fix

impl RecordWriter for RawFormatter {
    async fn write_record(
        record: &SequencedRecord,
        writer: &mut (impl AsyncWrite + Unpin),
    ) -> io::Result<()> {
        writer.write_all(&record.body).await  // <-- FIX 🟢 Write original bytes directly
    }
}

detail-app[bot] avatar Jan 15 '26 22:01 detail-app[bot]

by design

shikhar avatar Jan 25 '26 03:01 shikhar