comparison src/libpam/environ.rs @ 105:13b4d2a19674

Support Rust v1.75.0. This is the version included in Ubuntu 24.04 LTS and Debian Trixie, so it's old enough to have wide penetration without being too old to get new features (Debian Stable, I love you but v1.63 is just not going to work out).
author Paul Fisher <paul@pfish.zone>
date Thu, 26 Jun 2025 00:48:51 -0400
parents dfcd96a74ac4
children 49d9e2b5c189
comparison
equal deleted inserted replaced
104:a2676475e86b 105:13b4d2a19674
137 current: ptr.cast(), 137 current: ptr.cast(),
138 _owner: Default::default(), 138 _owner: Default::default(),
139 } 139 }
140 } 140 }
141 } 141 }
142
142 impl Iterator for EnvList<'_> { 143 impl Iterator for EnvList<'_> {
143 type Item = (OsString, OsString); 144 type Item = (OsString, OsString);
144 145
145 fn next(&mut self) -> Option<Self::Item> { 146 fn next(&mut self) -> Option<Self::Item> {
146 // SAFETY: We were given a pointer to a valid environment list, 147 // SAFETY: We were given a pointer to a valid environment list,
150 Some(item) => { 151 Some(item) => {
151 let ret = item.as_kv(); 152 let ret = item.as_kv();
152 // SAFETY: We know we're still pointing to a valid pointer, 153 // SAFETY: We know we're still pointing to a valid pointer,
153 // and advancing it one more is allowed. 154 // and advancing it one more is allowed.
154 unsafe { 155 unsafe {
155 self.current = self.current.add(1); 156 self.current = advance(self.current);
156 ptr::drop_in_place(item as *mut EnvVar); 157 ptr::drop_in_place(item as *mut EnvVar);
157 } 158 }
158 Some(ret) 159 Some(ret)
159 } 160 }
160 } 161 }
165 fn drop(&mut self) { 166 fn drop(&mut self) {
166 // SAFETY: We own self.start, and we know that self.current points to 167 // SAFETY: We own self.start, and we know that self.current points to
167 // either an item we haven't used, or to the None end. 168 // either an item we haven't used, or to the None end.
168 unsafe { 169 unsafe {
169 while let Some(var_ref) = self.current.as_mut() { 170 while let Some(var_ref) = self.current.as_mut() {
171 self.current = advance(self.current);
170 ptr::drop_in_place(var_ref as *mut EnvVar); 172 ptr::drop_in_place(var_ref as *mut EnvVar);
171 self.current = self.current.add(1);
172 } 173 }
173 memory::free(self.start.as_ptr()) 174 memory::free(self.start.as_ptr())
174 } 175 }
175 } 176 }
177 }
178
179 unsafe fn advance<T>(nn: NonNull<T>) -> NonNull<T> {
180 NonNull::new_unchecked(nn.as_ptr().offset(1))
176 } 181 }
177 182
178 struct EnvVar(CHeapString); 183 struct EnvVar(CHeapString);
179 184
180 impl EnvVar { 185 impl EnvVar {
215 fn env_list(strings: &[&'static str]) -> EnvList<'static> { 220 fn env_list(strings: &[&'static str]) -> EnvList<'static> {
216 let ptrs: NonNull<Option<CHeapString>> = memory::calloc(strings.len() + 1).unwrap(); 221 let ptrs: NonNull<Option<CHeapString>> = memory::calloc(strings.len() + 1).unwrap();
217 unsafe { 222 unsafe {
218 for (idx, &text) in strings.iter().enumerate() { 223 for (idx, &text) in strings.iter().enumerate() {
219 ptr::write( 224 ptr::write(
220 ptrs.add(idx).as_ptr(), 225 ptrs.as_ptr().add(idx),
221 Some(CHeapString::new(text).unwrap()), 226 Some(CHeapString::new(text).unwrap()),
222 ) 227 )
223 } 228 }
224 ptr::write(ptrs.add(strings.len()).as_ptr(), None); 229 ptr::write(ptrs.as_ptr().add(strings.len()), None);
225 EnvList::from_ptr(ptrs.cast()) 230 EnvList::from_ptr(ptrs.cast())
226 } 231 }
227 } 232 }
228 233
229 #[test] 234 #[test]