Document that CString::new sheds unused capacity from the buffer it's given #60558
Labels
A-docs
Area: documentation for any part of the project, including the compiler, standard library, and tools
A-ffi
Area: Foreign Function Interface (FFI)
C-enhancement
Category: An issue proposing an enhancement or a PR with one.
T-libs-api
Relevant to the library API team, which will review and decide on the PR/issue.
I tried to use
std::ffi::CString
as an output buffer forlibc::snprintf
like this (playground):I thought I'm creating a buffer of 1024 bytes, but in reality,
CString::new
appends a null byte and callsVec::into_boxed_slice
on my Vec, shrinking it to 1 byte. This leads to memory corruption, since I passsnprintf
a pointer to a one-byte buffer but tell it there are 1024 bytes available.I'm a bit unsure if
CString
is even a good choice here, but if it is, I thinkCString
's docs should be more explicit on what happens to the buffer. I suggest adding a comment toCString::new
doc saying the same asVec::into_boxed_slice
: "Note that this will drop any excess capacity".The text was updated successfully, but these errors were encountered: