@@ -25,6 +25,10 @@ impl CStringArray {
2525        let  argc = self . ptrs . len ( )  - 1 ; 
2626        let  ptr = & mut  self . ptrs [ ..argc] [ index] ; 
2727        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. 
2832        drop ( unsafe  {  CString :: from_raw ( old. cast_mut ( ) )  } ) ; 
2933    } 
3034
@@ -52,6 +56,9 @@ impl Index<usize> for CStringArray {
5256    type  Output  = CStr ; 
5357    fn  index ( & self ,  index :  usize )  -> & CStr  { 
5458        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. 
5562        unsafe  {  CStr :: from_ptr ( ptr)  } 
5663    } 
5764} 
@@ -69,6 +76,9 @@ unsafe impl Sync for CStringArray {}
6976
7077impl  Drop  for  CStringArray  { 
7178    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`. 
7282        self . ptrs [ ..self . ptrs . len ( )  - 1 ] 
7383            . iter ( ) 
7484            . for_each ( |& p| drop ( unsafe  {  CString :: from_raw ( p. cast_mut ( ) )  } ) ) 
@@ -84,6 +94,9 @@ pub struct CStringIter<'a> {
8494impl < ' a >  Iterator  for  CStringIter < ' a >  { 
8595    type  Item  = & ' a  CStr ; 
8696    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`. 
87100        self . iter . next ( ) . map ( |& p| unsafe  {  CStr :: from_ptr ( p)  } ) 
88101    } 
89102
0 commit comments