Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions library/std/src/sys/process/unix/common/cstring_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ impl CStringArray {
/// Push an additional string to the array.
pub fn push(&mut self, item: CString) {
let argc = self.ptrs.len() - 1;
Copy link
Copy Markdown
Contributor

@asder8215 asder8215 Apr 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, is it possible for self.ptrs.len() to be 0/should we be worried about underflow here?

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, since there is always at least one element (the null terminator) in the array.

// 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.
Expand Down
Loading