Skip to content

Commit 76c92f9

Browse files
committed
Avoid including null terminators
1 parent e216d5b commit 76c92f9

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

src/regex_impl.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,6 @@ impl<W: CodeUnitWidth> Regex<W> {
556556
let mut capacity = 256;
557557
let mut output: Vec<W::PCRE2_CHAR> = Vec::with_capacity(capacity);
558558
capacity = output.capacity();
559-
let mut saved_capacity = capacity;
560559

561560
let mut rc = unsafe {
562561
self.code
@@ -569,7 +568,6 @@ impl<W: CodeUnitWidth> Regex<W> {
569568
return Err(rc.unwrap_err());
570569
}
571570
capacity = output.capacity();
572-
saved_capacity = capacity;
573571
rc = unsafe {
574572
self.code.substitute(
575573
subject,
@@ -583,23 +581,25 @@ impl<W: CodeUnitWidth> Regex<W> {
583581
}
584582
}
585583

586-
Ok(match rc? {
584+
let s = match rc? {
587585
0 => Cow::Borrowed(subject),
588586
_ => {
589587
// +1 to account for null terminator
590-
let result = unsafe {
591-
Vec::from_raw_parts(output.as_mut_ptr(), capacity + 1, saved_capacity)
592-
};
593-
std::mem::forget(output);
594-
let x: Vec<W::SubjectChar> = result
588+
unsafe { output.set_len(capacity + 1) };
589+
590+
// this is really just a type cast
591+
let x: Vec<W::SubjectChar> = output
595592
.into_iter()
596593
.map(W::PCRE2_CHAR::try_into)
594+
// we don't want to return the null terminator
595+
.take(capacity)
597596
.collect::<Result<Vec<W::SubjectChar>, _>>()
598597
.expect("PCRE2 returned invalid characters");
599598

600599
Cow::Owned(x)
601600
}
602-
})
601+
};
602+
Ok(s)
603603
}
604604
}
605605

0 commit comments

Comments
 (0)