Skip to content

Commit e9607db

Browse files
Rollup merge of rust-lang#155774 - joboet:cstring_array_null, r=jhpratt
std: maintain `CStringArray` null-termination even if `Vec::push` panics Fixes rust-lang#155748 by performing the `push` of the new null terminator before overwriting the previous one.
2 parents 68ffae4 + bab4983 commit e9607db

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

library/std/src/sys/process/unix/common/cstring_array.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ impl CStringArray {
3535
/// Push an additional string to the array.
3636
pub fn push(&mut self, item: CString) {
3737
let argc = self.ptrs.len() - 1;
38-
// Replace the null pointer at the end of the array...
39-
self.ptrs[argc] = item.into_raw();
40-
// ... and recreate it to restore the data structure invariant.
38+
// Amend the array by another null pointer first, to ensure that the
39+
// array is null-terminated even when the `push` panics, in which case
40+
// the array will be left undisturbed (see #155748).
4141
self.ptrs.push(ptr::null());
42+
// Now, replace the previous null pointer.
43+
self.ptrs[argc] = item.into_raw();
4244
}
4345

4446
/// Returns a pointer to the C-string array managed by this type.

0 commit comments

Comments
 (0)