Skip to content

Fix UB in IOChunk::as_str(): only cache utf8_verified on successful UTF-8 validation#488

Closed
dspinellis with Copilot wants to merge 2 commits into
mainfrom
copilot/fix-code-for-review-comment
Closed

Fix UB in IOChunk::as_str(): only cache utf8_verified on successful UTF-8 validation#488
dspinellis with Copilot wants to merge 2 commits into
mainfrom
copilot/fix-code-for-review-comment

Conversation

Copilot AI commented Jul 7, 2026

Copy link
Copy Markdown

IOChunk::as_str() set utf8_verified = true unconditionally — even when str::from_utf8 failed. A subsequent call would then hit the cached branch and invoke as_str_unchecked() on invalid UTF-8, which is undefined behavior.

Change

  • src/sed/fast_io.rs: Replace the unconditional utf8_verified.set(true) with a match on the str::from_utf8 result, only setting the flag in the Ok arm.
// Before
let result = str::from_utf8(content);
self.utf8_verified.set(true);          // ← set even on Err
result.map_err(|e| USimpleError::new(2, e.to_string()))

// After
match str::from_utf8(content) {
    Ok(s) => {
        self.utf8_verified.set(true);  // ← only on success
        Ok(s)
    }
    Err(e) => Err(USimpleError::new(2, e.to_string())),
}

Copilot AI changed the title [WIP] Fix the code based on review comment Fix UB in IOChunk::as_str(): only cache utf8_verified on successful UTF-8 validation Jul 7, 2026
Copilot AI requested a review from dspinellis July 7, 2026 12:42
@dspinellis

Copy link
Copy Markdown
Collaborator

Incorporated into #487.

@dspinellis dspinellis closed this Jul 7, 2026
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