Skip to content

Commit 849747b

Browse files
committed
Make internal OsString::truncate and extend_from_slice unsafe
Communicate the safety invariants of these methods with `unsafe fn` rather than privacy.
1 parent 9bfa31f commit 849747b

File tree

4 files changed

+47
-25
lines changed

4 files changed

+47
-25
lines changed

library/std/src/ffi/os_str.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -582,15 +582,21 @@ impl OsString {
582582
#[unstable(feature = "os_string_truncate", issue = "133262")]
583583
pub fn truncate(&mut self, len: usize) {
584584
self.as_os_str().inner.check_public_boundary(len);
585-
self.inner.truncate(len);
585+
// SAFETY: The length was just checked to be at a valid boundary.
586+
unsafe { self.inner.truncate_unchecked(len) };
586587
}
587588

588-
/// Provides plumbing to core `Vec::extend_from_slice`.
589-
/// More well behaving alternative to allowing outer types
590-
/// full mutable access to the core `Vec`.
589+
/// Provides plumbing to `Vec::extend_from_slice` without giving full
590+
/// mutable access to the `Vec`.
591+
///
592+
/// # Safety
593+
///
594+
/// Extending the buffer with this slice must not require encoding-dependent
595+
/// surrogate joining.
591596
#[inline]
592-
pub(crate) fn extend_from_slice(&mut self, other: &[u8]) {
593-
self.inner.extend_from_slice(other);
597+
pub(crate) unsafe fn extend_from_slice_unchecked(&mut self, other: &[u8]) {
598+
// SAFETY: Guaranteed by caller.
599+
unsafe { self.inner.extend_from_slice_unchecked(other) };
594600
}
595601
}
596602

library/std/src/path.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2759,7 +2759,8 @@ impl Path {
27592759
};
27602760

27612761
let mut new_path = PathBuf::with_capacity(new_capacity);
2762-
new_path.inner.extend_from_slice(slice_to_copy);
2762+
// SAFETY: The path is empty, so cannot have surrogate halves.
2763+
unsafe { new_path.inner.extend_from_slice_unchecked(slice_to_copy) };
27632764
new_path.set_extension(extension);
27642765
new_path
27652766
}

library/std/src/sys/os_str/bytes.rs

+16-9
Original file line numberDiff line numberDiff line change
@@ -216,19 +216,26 @@ impl Buf {
216216
self.as_slice().into_rc()
217217
}
218218

219-
/// Provides plumbing to core `Vec::truncate`.
220-
/// More well behaving alternative to allowing outer types
221-
/// full mutable access to the core `Vec`.
222-
#[inline]
223-
pub(crate) fn truncate(&mut self, len: usize) {
219+
/// Provides plumbing to `Vec::truncate` without giving full mutable access
220+
/// to the `Vec`.
221+
///
222+
/// # Safety
223+
///
224+
/// The length must be at an `OsStr` boundary, according to
225+
/// `Slice::check_public_boundary`.
226+
#[inline]
227+
pub unsafe fn truncate_unchecked(&mut self, len: usize) {
224228
self.inner.truncate(len);
225229
}
226230

227-
/// Provides plumbing to core `Vec::extend_from_slice`.
228-
/// More well behaving alternative to allowing outer types
229-
/// full mutable access to the core `Vec`.
231+
/// Provides plumbing to `Vec::extend_from_slice` without giving full
232+
/// mutable access to the `Vec`.
233+
///
234+
/// # Safety
235+
///
236+
/// This encoding has no safety requirements.
230237
#[inline]
231-
pub(crate) fn extend_from_slice(&mut self, other: &[u8]) {
238+
pub unsafe fn extend_from_slice_unchecked(&mut self, other: &[u8]) {
232239
self.inner.extend_from_slice(other);
233240
}
234241
}

library/std/src/sys/os_str/wtf8.rs

+17-9
Original file line numberDiff line numberDiff line change
@@ -195,19 +195,27 @@ impl Buf {
195195
self.as_slice().into_rc()
196196
}
197197

198-
/// Provides plumbing to core `Vec::truncate`.
199-
/// More well behaving alternative to allowing outer types
200-
/// full mutable access to the core `Vec`.
201-
#[inline]
202-
pub(crate) fn truncate(&mut self, len: usize) {
198+
/// Provides plumbing to `Vec::truncate` without giving full mutable access
199+
/// to the `Vec`.
200+
///
201+
/// # Safety
202+
///
203+
/// The length must be at an `OsStr` boundary, according to
204+
/// `Slice::check_public_boundary`.
205+
#[inline]
206+
pub unsafe fn truncate_unchecked(&mut self, len: usize) {
203207
self.inner.truncate(len);
204208
}
205209

206-
/// Provides plumbing to core `Vec::extend_from_slice`.
207-
/// More well behaving alternative to allowing outer types
208-
/// full mutable access to the core `Vec`.
210+
/// Provides plumbing to `Vec::extend_from_slice` without giving full
211+
/// mutable access to the `Vec`.
212+
///
213+
/// # Safety
214+
///
215+
/// Extending the buffer with this slice must not require joining
216+
/// surrogates.
209217
#[inline]
210-
pub(crate) fn extend_from_slice(&mut self, other: &[u8]) {
218+
pub unsafe fn extend_from_slice_unchecked(&mut self, other: &[u8]) {
211219
self.inner.extend_from_slice(other);
212220
}
213221
}

0 commit comments

Comments
 (0)