@@ -25,6 +25,10 @@ impl CStringArray {
25
25
let argc = self . ptrs . len ( ) - 1 ;
26
26
let ptr = & mut self . ptrs [ ..argc] [ index] ;
27
27
let old = mem:: replace ( ptr, item. into_raw ( ) ) ;
28
+ // SAFETY:
29
+ // `CStringArray` owns all of its strings, and they were all transformed
30
+ // into pointers using `CString::into_raw`. Also, this is not the null
31
+ // pointer since the indexing above would have failed.
28
32
drop ( unsafe { CString :: from_raw ( old. cast_mut ( ) ) } ) ;
29
33
}
30
34
@@ -52,6 +56,9 @@ impl Index<usize> for CStringArray {
52
56
type Output = CStr ;
53
57
fn index ( & self , index : usize ) -> & CStr {
54
58
let ptr = self . ptrs [ ..self . ptrs . len ( ) - 1 ] [ index] ;
59
+ // SAFETY:
60
+ // `CStringArray` owns all of its strings. Also, this is not the null
61
+ // pointer since the indexing above would have failed.
55
62
unsafe { CStr :: from_ptr ( ptr) }
56
63
}
57
64
}
@@ -69,6 +76,9 @@ unsafe impl Sync for CStringArray {}
69
76
70
77
impl Drop for CStringArray {
71
78
fn drop ( & mut self ) {
79
+ // SAFETY:
80
+ // `CStringArray` owns all of its strings, and they were all transformed
81
+ // into pointers using `CString::into_raw`.
72
82
self . ptrs [ ..self . ptrs . len ( ) - 1 ]
73
83
. iter ( )
74
84
. for_each ( |& p| drop ( unsafe { CString :: from_raw ( p. cast_mut ( ) ) } ) )
@@ -84,6 +94,9 @@ pub struct CStringIter<'a> {
84
94
impl < ' a > Iterator for CStringIter < ' a > {
85
95
type Item = & ' a CStr ;
86
96
fn next ( & mut self ) -> Option < & ' a CStr > {
97
+ // SAFETY:
98
+ // `CStringArray` owns all of its strings. Also, this is not the null
99
+ // pointer since the last element is excluded when creating `iter`.
87
100
self . iter . next ( ) . map ( |& p| unsafe { CStr :: from_ptr ( p) } )
88
101
}
89
102
0 commit comments