ExpHP / rsp2

phonons in rust
Apache License 2.0
2 stars 1 forks source link

current usage of CString technically invokes undefined behavior #46

Closed ExpHP closed 6 years ago

ExpHP commented 6 years ago

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...)


Aside: wtf is the point of CString!?