hkrn / nanoem

nanoem is an MMD (MikuMikuDance) compatible implementation and its like cross-platform application mainly built for macOS.
https://nanoem.rtfd.io
Other
281 stars 41 forks source link

Replace transmute to box from/into #503

Open noname0310 opened 1 month ago

noname0310 commented 1 month ago

Summary

According to the rust documentation , std::mem::transmute is a dangerous function that is very prone to creating undefined behavior and should only be used as a last resort:

Both the argument and the result must be valid at their given type.
Violating this condition leads to undefined behavior.
The compiler will generate code assuming that you, the programmer, ensure that there will never be undefined behavior.
It is therefore your responsibility to guarantee that every value passed to transmute is valid at both types Src and Dst.
Failing to uphold this condition may lead to unexpected and unstable compilation results.
This makes transmute incredibly unsafe. transmute should be the absolute last resort.

And all the transmute functions used so far in this project are simply being used to extend the lifetime of a boxed value. This scenario is common in FFI code, so there are already better functions than std::mem::transmute for this. For example, you can use the pair Box::into_raw and Box::from_raw.

This pull request replaces all std::mem::transmute calls with Box::into_raw and Box::from_raw to make the code safer.

Note

hkrn commented 2 weeks ago

The changes themselves seem good, but they may require some adjustments considering Rust 2024 compatibility.

https://doc.rust-lang.org/nightly/edition-guide/rust-2024/unsafe-op-in-unsafe-fn.html

hkrn commented 2 weeks ago

As noted in the pull request template, signatures are required for "every commit". Since this requirement has not been met, I'm unable to merge the pull request.

noname0310 commented 2 weeks ago

I'm sure there's no problem now