Skip to content

Commit

Permalink
mark Code::substitute as unsafe. As it allows passing options.
Browse files Browse the repository at this point in the history
  • Loading branch information
navneetankur committed Oct 4, 2024
1 parent 446570d commit 75614e8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
18 changes: 11 additions & 7 deletions src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,8 @@ impl Regex {
replacement: &[u8],
output: &mut Vec<u8>,
) -> Result<usize, Error> {
self.code.substitute(subject, replacement, output, 0)
// Safety: options is 0.
unsafe { self.code.substitute(subject, replacement, output, 0) }
}
/// Replaces all the matches in `subject` with the `replacement`,
/// and puts the replaced string in `output`.
Expand All @@ -633,12 +634,15 @@ impl Regex {
replacement: &[u8],
output: &mut Vec<u8>,
) -> Result<usize, Error> {
self.code.substitute(
subject,
replacement,
output,
pcre2_sys::PCRE2_SUBSTITUTE_GLOBAL,
)
// Safety: PCRE2_SUBSTITUTE_GLOBAL is not a dangerous option.
unsafe {
self.code.substitute(
subject,
replacement,
output,
pcre2_sys::PCRE2_SUBSTITUTE_GLOBAL,
)
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,9 @@ impl Code {

/// Substitute the replacement pattern in subject and put the output in
/// output vec. Output vec is will be cleared before use.
pub(crate) fn substitute(
/// Safety: ensure that options field will not cause undefined behavior
/// in pcre2-sys.
pub(crate) unsafe fn substitute(
&self,
subject: &[u8],
replacement: &[u8],
Expand Down

0 comments on commit 75614e8

Please sign in to comment.