codex - 💡(How to fix) Fix Bug: in apply-patch silently swallows mismatched replacement ranges

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

In codex-rs/apply-patch/src/lib.rs, the apply_replacements function at line 544 has a guard that silently skips removal when start_idx >= lines.len():

for _ in 0..old_len {
    if start_idx < lines.len() {
        lines.remove(start_idx);
    }
}

When start_idx >= lines.len(), the loop silently does nothing — it does not remove the expected old_len lines but still proceeds to insert new_segment at start_idx. This means a patch with a stale or incorrect range will produce silently corrupted output rather than an error.

Error Message

for _ in 0..old_len { if start_idx >= lines.len() { return /* error: replacement range exceeds file length */; } lines.remove(start_idx); }

Root Cause

In codex-rs/apply-patch/src/lib.rs, the apply_replacements function at line 544 has a guard that silently skips removal when start_idx >= lines.len():

for _ in 0..old_len {
    if start_idx < lines.len() {
        lines.remove(start_idx);
    }
}

When start_idx >= lines.len(), the loop silently does nothing — it does not remove the expected old_len lines but still proceeds to insert new_segment at start_idx. This means a patch with a stale or incorrect range will produce silently corrupted output rather than an error.

Fix Action

Fix / Workaround

In codex-rs/apply-patch/src/lib.rs, the apply_replacements function at line 544 has a guard that silently skips removal when start_idx >= lines.len():

When start_idx >= lines.len(), the loop silently does nothing — it does not remove the expected old_len lines but still proceeds to insert new_segment at start_idx. This means a patch with a stale or incorrect range will produce silently corrupted output rather than an error.

Environment

  • Repository: openai/codex, main branch
  • File: codex-rs/apply-patch/src/lib.rs, lines 539-554

Code Example

for _ in 0..old_len {
    if start_idx < lines.len() {
        lines.remove(start_idx);
    }
}

---

for _ in 0..old_len {
    if start_idx >= lines.len() {
        return /* error: replacement range exceeds file length */;
    }
    lines.remove(start_idx);
}
RAW_BUFFERClick to expand / collapse

Description

In codex-rs/apply-patch/src/lib.rs, the apply_replacements function at line 544 has a guard that silently skips removal when start_idx >= lines.len():

for _ in 0..old_len {
    if start_idx < lines.len() {
        lines.remove(start_idx);
    }
}

When start_idx >= lines.len(), the loop silently does nothing — it does not remove the expected old_len lines but still proceeds to insert new_segment at start_idx. This means a patch with a stale or incorrect range will produce silently corrupted output rather than an error.

Expected behavior

If the replacement range is out of bounds, the function should return an error rather than silently producing incorrect output.

Suggested fix

for _ in 0..old_len {
    if start_idx >= lines.len() {
        return /* error: replacement range exceeds file length */;
    }
    lines.remove(start_idx);
}

Or alternatively, validate all replacement ranges against lines.len() before entering the loop.

Environment

  • Repository: openai/codex, main branch
  • File: codex-rs/apply-patch/src/lib.rs, lines 539-554

extent analysis

TL;DR

The issue can be fixed by adding a bounds check for start_idx to prevent silent corruption and return an error when the replacement range exceeds the file length.

Guidance

  • Add a check at the beginning of the loop to ensure start_idx is within bounds of lines.len() to prevent silent skipping of removals.
  • Consider validating all replacement ranges against lines.len() before entering the loop for additional safety.
  • The suggested fix provided in the issue body can be used as a starting point, with the error handling implemented according to the project's standards.
  • Review the function's documentation and tests to ensure they reflect the expected behavior and error handling.

Example

for _ in 0..old_len {
    if start_idx >= lines.len() {
        return Err("Replacement range exceeds file length");
    }
    lines.remove(start_idx);
}

Notes

The provided fix assumes that the project uses a Result-based error handling system. If a different error handling mechanism is used, the fix should be adapted accordingly.

Recommendation

Apply the suggested fix with proper error handling to ensure the function behaves as expected and returns an error when the replacement range is out of bounds. This will prevent silently corrupted output and improve the overall reliability of the apply_replacements function.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

FAQ

Expected behavior

If the replacement range is out of bounds, the function should return an error rather than silently producing incorrect output.

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

codex - 💡(How to fix) Fix Bug: in apply-patch silently swallows mismatched replacement ranges