You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Lints against usages of String::from_raw_parts. This should be done in two steps, Vec::from_raw_parts and String::from_utf8_unchecked.
rust-lang/rust#136775 updated String::from_raw_parts's docs to delegate the safety requirements to Vec::from_raw_parts's and String::from_utf8_unchecked's docs anyway.
Advantage
String::from_raw_parts has conflated safety requirements that should be justified in two steps:
Vec::from_raw_parts has complicated language-level safety requirements that have to be manually justified and are insta-UB (?) if violated
String::from_utf8_unchecked has a relatively trivial library-level safety requirement, that could be checked by using String::from_utf8 instead
Drawbacks
No response
Example
// SAFETY: // * `ptr` was allocated using the global allocator// * `T` (= `u8`) has the same alignment as what `ptr` was allocated with// * The size of T (= u8) times the capacity is the same size as `ptr` was allocated with// * `len` is use for both `len` and `capacity`// * The first `len` values are properly initialized values of type `T` (= `u8`).// * `capacity` (= `len`) is the capacity that `ptr` was allocated with.// * The allocated size in bytes is no larger than `isize::MAX`// * The bytes contain valid UTF-8let string = unsafe{String::from_raw_parts(pointer, len)};
Could be written as:
// SAFETY: // * `ptr` was allocated using the global allocator// * `T` (= `u8`) has the same alignment as what `ptr` was allocated with// * The size of T (= u8) times the capacity is the same size as `ptr` was allocated with// * `len` is use for both `len` and `capacity`// * The first `len` values are properly initialized values of type `T` (= `u8`).// * `capacity` (= `len`) is the capacity that `ptr` was allocated with.// * The allocated size in bytes is no larger than `isize::MAX`let bytes = unsafe{Vec::from_raw_parts(ptr, len, len)};// SAFETY: // * The bytes contain valid UTF-8let string = unsafe{String::from_utf8_unchecked(bytes)};
The text was updated successfully, but these errors were encountered:
Note that you can already warn about or deny using String::from_raw_parts() by using the clippy::disallowed_methods lint. You can even indicate the reason for disallowing it.
What it does
Lints against usages of
String::from_raw_parts
. This should be done in two steps,Vec::from_raw_parts
andString::from_utf8_unchecked
.rust-lang/rust#136775 updated
String::from_raw_parts
's docs to delegate the safety requirements toVec::from_raw_parts
's andString::from_utf8_unchecked
's docs anyway.Advantage
String::from_raw_parts
has conflated safety requirements that should be justified in two steps:Vec::from_raw_parts
has complicated language-level safety requirements that have to be manually justified and are insta-UB (?) if violatedString::from_utf8_unchecked
has a relatively trivial library-level safety requirement, that could be checked by usingString::from_utf8
insteadDrawbacks
No response
Example
Could be written as:
The text was updated successfully, but these errors were encountered: