Fix use-after-free: bind Regex lifetime to custom Syntax via PhantomData#222
Merged
Merged
Conversation
…afety Agent-Logs-Url: https://github.com/rust-onig/rust-onig/sessions/a640bd63-c87f-4d29-b05e-70c57e993daa Co-authored-by: iwillspeak <1004401+iwillspeak@users.noreply.github.com>
…bounds Agent-Logs-Url: https://github.com/rust-onig/rust-onig/sessions/a640bd63-c87f-4d29-b05e-70c57e993daa Co-authored-by: iwillspeak <1004401+iwillspeak@users.noreply.github.com>
Collaborator
|
What do we think @Keats ? Adds a lifetime requirement to |
Copilot
AI
changed the title
[WIP] Fix potential lifetime mismatch in Regex::with_options
Fix use-after-free: bind Apr 26, 2026
Regex lifetime to custom Syntax via PhantomData
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the onig crate’s Regex type to carry an explicit lifetime tying it to the Syntax used at compile time, preventing a compiled regex from outliving a custom (non-static) Syntax.
Changes:
- Introduce
Regex<'syntax>with aPhantomData<&'syntax Syntax>marker to enforce correct syntax lifetimes. - Update constructors (
new,with_encoding,with_options,with_options_and_encoding) to returnRegexvalues with appropriate lifetimes ('staticfor default syntax, otherwise tied to the provided&Syntax). - Propagate the new
Regex<'syntax>type through related impl blocks and add a test demonstrating the lifetime enforcement.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| onig/src/lib.rs | Adds Regex<'syntax> + PhantomData, updates constructors’ return types, updates Drop/Send/Sync, and adds a lifetime-enforcement test. |
| onig/src/find.rs | Adjusts Regex impl block to be generic over 'syntax (propagating the new Regex<'syntax> type). |
| onig/src/pattern.rs | Updates Pattern integration to work with the lifetime-parameterized Regex. |
| onig/src/names.rs | Updates inherent impl block to Regex<'syntax>. |
| onig/src/replace.rs | Updates inherent impl block to Regex<'syntax>. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
iwillspeak
approved these changes
Apr 26, 2026
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Contributor
|
I think that's fine? 99% of the usage will be 'static anyway |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The Oniguruma C engine permanently stores the
syntaxpointer insidere_pattern_buffer(viaonig_reg_init), but the safe Rust API did not express this dependency — allowing a customSyntaxto be dropped while theRegexwas still live, yielding a dangling pointer.Approach
Add a
'syntaxlifetime parameter toRegexbacked byPhantomData<&'syntax Syntax>, surfacing the C-level memory dependency to the borrow checker at zero runtime cost.Changes
Regex<'syntax>— new lifetime parameter +_syntax: PhantomData<&'syntax Syntax>field;Send/Sync/Dropimpls updated accordingly.new/with_encoding→ explicitly returnRegex<'static>(useSyntax::default()which is'static; no new restriction on callers)with_options<'s>(..., syntax: &'s Syntax) -> Result<Regex<'s>, Error>— returnedRegexcannot outlivesyntaxwith_options_and_encoding<'s, T>(..., syntax: &'s Syntax) -> Result<Regex<'s>, Error>— same'syntaxadded as an independent lifetime parameter (separate from the regex reference lifetime'r):FindMatches<'r, 'syntax, 't>,FindCaptures<'r, 'syntax, 't>,RegexSplits<'r, 'syntax, 't>,RegexSplitsN<'r, 'syntax, 't>pattern.rs—RegexSearcher<'r, 'syntax, 'a>;impl<'r, 'syntax> Pattern for &'r Regex<'syntax>Example
Callers using
Regex::neworRegex::with_options(..., Syntax::default())are unaffected — the returned type isRegex<'static>.