mirror of
https://github.com/rust-lang/rust.git
synced 2026-04-27 18:57:42 +03:00
std: maintain CStringArray null-termination even if Vec::push panics
This commit is contained in:
@@ -35,10 +35,12 @@ pub fn write(&mut self, index: usize, item: CString) {
|
||||
/// Push an additional string to the array.
|
||||
pub fn push(&mut self, item: CString) {
|
||||
let argc = self.ptrs.len() - 1;
|
||||
// Replace the null pointer at the end of the array...
|
||||
self.ptrs[argc] = item.into_raw();
|
||||
// ... and recreate it to restore the data structure invariant.
|
||||
// Amend the array by another null pointer first, to ensure that the
|
||||
// array is null-terminated even when the `push` panics, in which case
|
||||
// the array will be left undisturbed (see #155748).
|
||||
self.ptrs.push(ptr::null());
|
||||
// Now, replace the previous null pointer.
|
||||
self.ptrs[argc] = item.into_raw();
|
||||
}
|
||||
|
||||
/// Returns a pointer to the C-string array managed by this type.
|
||||
|
||||
Reference in New Issue
Block a user