From 75614e8449797ecb3958d3a7831c92d7959292db Mon Sep 17 00:00:00 2001 From: Navneet Aman Date: Fri, 4 Oct 2024 12:38:30 +0530 Subject: [PATCH] mark Code::substitute as unsafe. As it allows passing options. --- src/bytes.rs | 18 +++++++++++------- src/ffi.rs | 4 +++- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/bytes.rs b/src/bytes.rs index 02755fa..03ef45a 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -610,7 +610,8 @@ impl Regex { replacement: &[u8], output: &mut Vec, ) -> Result { - 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`. @@ -633,12 +634,15 @@ impl Regex { replacement: &[u8], output: &mut Vec, ) -> Result { - 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, + ) + } } } diff --git a/src/ffi.rs b/src/ffi.rs index c2f8f77..0a032b3 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -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],