Skip to content

Commit 90abe2d

Browse files
authored
Merge pull request #93 from clubby789/fix_ub
Fix UB in documentation example
2 parents a6f9372 + 6ecfc99 commit 90abe2d

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

.github/workflows/ci.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,13 @@ jobs:
4545
with:
4646
rust-version: nightly
4747
- run: cargo test -Zminimal-versions --verbose --all-features
48+
49+
miri:
50+
name: Run tests under `miri` to check for UB
51+
runs-on: ubuntu-latest
52+
steps:
53+
- uses: actions/checkout@v3
54+
- uses: dtolnay/rust-toolchain@nightly
55+
with:
56+
components: miri
57+
- run: cargo miri test --all-features

src/ascii_string.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,14 @@ impl AsciiString {
6464
/// This is highly unsafe, due to the number of invariants that aren't checked:
6565
///
6666
/// * The memory at `buf` need to have been previously allocated by the same allocator this
67-
/// library uses.
67+
/// library uses, with an alignment of 1.
6868
/// * `length` needs to be less than or equal to `capacity`.
6969
/// * `capacity` needs to be the correct value.
7070
/// * `buf` must have `length` valid ascii elements and contain a total of `capacity` total,
7171
/// possibly, uninitialized, elements.
72+
/// * Nothing else must be using the memory `buf` points to.
7273
///
73-
/// Violating these may cause problems like corrupting the allocator's internal datastructures.
74+
/// Violating these may cause problems like corrupting the allocator's internal data structures.
7475
///
7576
/// # Examples
7677
///
@@ -81,14 +82,14 @@ impl AsciiString {
8182
/// use std::mem;
8283
///
8384
/// unsafe {
84-
/// let s = AsciiString::from_ascii("hello").unwrap();
85-
/// let ptr = s.as_ptr();
85+
/// let mut s = AsciiString::from_ascii("hello").unwrap();
86+
/// let ptr = s.as_mut_ptr();
8687
/// let len = s.len();
8788
/// let capacity = s.capacity();
8889
///
8990
/// mem::forget(s);
9091
///
91-
/// let s = AsciiString::from_raw_parts(ptr as *mut _, len, capacity);
92+
/// let s = AsciiString::from_raw_parts(ptr, len, capacity);
9293
///
9394
/// assert_eq!(AsciiString::from_ascii("hello").unwrap(), s);
9495
/// }
@@ -97,9 +98,9 @@ impl AsciiString {
9798
#[must_use]
9899
pub unsafe fn from_raw_parts(buf: *mut AsciiChar, length: usize, capacity: usize) -> Self {
99100
AsciiString {
100-
// SAFETY: Caller guarantees `buf` was previously allocated by this library,
101-
// that `buf` contains `length` valid ascii elements and has a total
102-
// capacity of `capacity` elements.
101+
// SAFETY: Caller guarantees that `buf` was previously allocated by this library,
102+
// that `buf` contains `length` valid ascii elements and has a total capacity
103+
// of `capacity` elements, and that nothing else is using the momory.
103104
vec: unsafe { Vec::from_raw_parts(buf, length, capacity) },
104105
}
105106
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy