about summary refs log tree commit diff
path: root/src/docs
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-10-23 17:17:32 +0000
committerbors <bors@rust-lang.org>2022-10-23 17:17:32 +0000
commit191c9839f0bad1c2bfec17d55bacb94d2e83f1a1 (patch)
treeccb6b90b4b4bf7ffb156856745706b5c53e0bbc0 /src/docs
parent628a79d6b67cc2acd2019f6c3c8e5adbc4384c9c (diff)
parentb083a39190721829a72779021011da8483dc0df8 (diff)
Auto merge of #9690 - royrustdev:boxed_void, r=flip1995
add `from_raw_with_void_ptr` lint

This PR `fixes #9679`

- \[x] Followed [lint naming conventions][lint_naming]
- \[x] Added passing UI tests (including committed `.stderr` file)
- \[x] `cargo test` passes locally
- \[x] Executed `cargo dev update_lints`
- \[x] Added lint documentation
- \[x] Run `cargo dev fmt`

---

changelog:  [`from_raw_with_void_ptr`]: added new lint
Diffstat (limited to 'src/docs')
-rw-r--r--src/docs/from_raw_with_void_ptr.txt22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/docs/from_raw_with_void_ptr.txt b/src/docs/from_raw_with_void_ptr.txt
new file mode 100644
index 00000000000..755fff23955
--- /dev/null
+++ b/src/docs/from_raw_with_void_ptr.txt
@@ -0,0 +1,22 @@
+### What it does
+Checks if we're passing a `c_void` raw pointer to `Box::from_raw(_)`
+
+### Why is this bad?
+However, it is easy to run into the pitfall of calling from_raw with the c_void pointer.
+Note that the definition of, say, Box::from_raw is:
+
+`pub unsafe fn from_raw(raw: *mut T) -> Box<T>`
+
+meaning that if you pass a *mut c_void you will get a Box<c_void>.
+Per the safety requirements in the documentation, for this to be safe,
+c_void would need to have the same memory layout as the original type, which is often not the case.
+
+### Example
+```
+let ptr = Box::into_raw(Box::new(42usize)) as *mut c_void;
+let _ = unsafe { Box::from_raw(ptr) };
+```
+Use instead:
+```
+let _ = unsafe { Box::from_raw(ptr as *mut usize) };
+```