Skip to content

fix: re.sub backslash-escape crash in PR comment section replace - #82

Merged
JarbasAl merged 1 commit into
devfrom
fix/re-sub-backslash-escape-crash
Aug 11, 2026
Merged

fix: re.sub backslash-escape crash in PR comment section replace#82
JarbasAl merged 1 commit into
devfrom
fix/re-sub-backslash-escape-crash

Conversation

@JarbasAl

@JarbasAl JarbasAl commented Aug 11, 2026

Copy link
Copy Markdown
Member

🤖 Auto-generated by Claude Fable 5 (claude-fable-5) via Claude Code — NOT human-reviewed. Verify before acting.

Bug

scripts/update_pr_comment.py, insert_or_replace_section() (line 1135), called:

return re.sub(pattern, new_section, body, flags=re.DOTALL)

new_section is built by build_section() from arbitrary generated content (test output, coverage numbers, bus-message payloads, etc.) and passed to re.sub as a string replacement. re.sub interprets backslashes in a string replacement as escape/backreference syntax, so content containing sequences like \x or \1 raised:

re.error: bad escape \x at position N

This reddened CI in downstream repos whenever a check's generated section content happened to contain a backslash sequence. Observed deterministically on ovos-skill-volume PR #129's bus-coverage comment step, where the section content included raw \x byte sequences from the bus message dump.

Fix

Pass a callable (lambda _match: new_section) as the replacement instead of a raw string, so re.sub inserts the content verbatim without interpreting any backslash sequences in it. This was the only re.sub/re.subn call in the file with a dynamic replacement argument; audited the rest of the module and found no other instances.

Test evidence

Added TestInsertOrReplaceSectionBackslashSafety in test/test_update_pr_comment.py covering:

  • replace-path content containing \x00, \x01, \x4F
  • replace-path content containing backreference-like sequences (\1, \g<name>)
  • append-path (no existing section) content containing backslashes

Red (before fix, on unmodified insert_or_replace_section):

FAILED test/test_update_pr_comment.py::TestInsertOrReplaceSectionBackslashSafety::test_replace_with_backslash_x_does_not_raise
FAILED test/test_update_pr_comment.py::TestInsertOrReplaceSectionBackslashSafety::test_replace_with_backreference_like_sequence_does_not_raise
re.error: bad escape \x at position 113 (line 6, column 11)
2 failed, 1 passed, 37 deselected in 0.22s

Green (after fix):

40 passed in 93.73s (0:01:33)

Full existing suite (40 tests) still passes after the fix.

Summary by CodeRabbit

  • Bug Fixes

    • Fixed an issue where generated content containing backslashes or backreference-like sequences could fail during comment updates.
    • Section replacement and appending now preserve such content correctly.
  • Tests

    • Added regression coverage for updating sections with special character sequences.

🤖 Independent review added by Claude Sonnet 5 (claude-sonnet-5) via Claude Code — NOT human-reviewed. Verify before acting.

Independent review

Verified against current source (origin/dev diff as posted):

  • Confirmed via gh api that this is the only re.sub/re.subn call with a dynamic replacement arg in scripts/update_pr_comment.py (grepped full file: only one re.sub site, at the fixed line).
  • The lambda _match: new_section fix is the textbook-correct way to avoid re.sub interpreting backslashes/backreferences in the replacement string — it does not change matching behavior (pattern/re.DOTALL untouched), only how the replacement is substituted. No over-matching risk introduced.
  • Regression test (TestInsertOrReplaceSectionBackslashSafety) exercises both the replace-path and append-path with \x, \1, \g<name> sequences — this is a real regression test, not just a happy-path add.
  • CI green on all 5 Python versions (3.10–3.14); CodeRabbit review completed with no findings (only the auto-summary comment, folded into this PR's own body above).
  • Scope check: no other automation path touched — single-file logic change + tests.

Issue list: none.

Verdict: MERGE
Safe for the human to merge? Yes — correctness fix, real regression test (fails before/passes after per PR's own red/green evidence), CI green, no other paths touched.

insert_or_replace_section() passed generated section content directly as
the replacement argument to re.sub(). re.sub interprets backslashes in a
string replacement as escape/backreference syntax, so content containing
sequences like "\x" or "\1" (e.g. raw test output, byte-string reprs)
raised re.error: bad escape \x, reddening downstream CI. Observed
deterministically on ovos-skill-volume PR #129's bus-coverage comment step.

Fix by using a lambda replacement so the content is inserted verbatim.
@coderabbitai

coderabbitai Bot commented Aug 11, 2026

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: a2b07bd4-159d-4adc-9482-9db67acd6253

📥 Commits

Reviewing files that changed from the base of the PR and between dc48bdf and 376b303.

📒 Files selected for processing (2)
  • scripts/update_pr_comment.py
  • test/test_update_pr_comment.py

📝 Walkthrough

Walkthrough

insert_or_replace_section now preserves literal backslash sequences when it replaces or appends content. Regression tests cover escape and backreference-like sequences.

Changes

Section replacement safety

Layer / File(s) Summary
Literal replacement and regression coverage
scripts/update_pr_comment.py, test/test_update_pr_comment.py
insert_or_replace_section uses a lambda replacement function. Tests verify that \x, \1, and \g<name> remain unchanged during replacement and append operations.

Estimated code review effort: 2 (Simple) | ~10 minutes

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/re-sub-backslash-escape-crash

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@JarbasAl
JarbasAl marked this pull request as ready for review August 11, 2026 17:19
@JarbasAl
JarbasAl merged commit 285279b into dev Aug 11, 2026
6 checks passed
@JarbasAl
JarbasAl deleted the fix/re-sub-backslash-escape-crash branch August 11, 2026 17:20
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.

1 participant