std::ffi::CString has an (IMO crippling) invariant that the length of the allocated buffer is exactly the correct size to hold the null-terminated string. from_raw recomputes the length by searching for the NUL, and the recomputed length is given to the allocator on deallocation with the assumption that it matches the original length.
This means that using CString::{into_raw,from_raw} to send string commands to Lammps is actually undefined behavior, because Lammps changes the length of the string by writing a NUL byte after the command name.
For managing the deallocation of these strings properly, lammps-wrap should be using Vec<u8> to back the memory instead of CString. (go figure, Vec<u8> is easier to use anyways...)
std::ffi::CString
has an (IMO crippling) invariant that the length of the allocated buffer is exactly the correct size to hold the null-terminated string.from_raw
recomputes the length by searching for the NUL, and the recomputed length is given to the allocator on deallocation with the assumption that it matches the original length.This means that using
CString::{into_raw,from_raw}
to send string commands to Lammps is actually undefined behavior, because Lammps changes the length of the string by writing a NUL byte after the command name.For managing the deallocation of these strings properly,
lammps-wrap
should be usingVec<u8>
to back the memory instead ofCString
. (go figure,Vec<u8>
is easier to use anyways...)Aside: wtf is the point of
CString
!?