Skip to content

Commit 3175c4f

Browse files
author
Alexander Krotov
authored
Merge pull request #333 from link2xt/dc_array-assert
dc_array: panic on null pointers and out of range indexes
2 parents a29f06a + 4d402f3 commit 3175c4f

File tree

1 file changed

+53
-92
lines changed

1 file changed

+53
-92
lines changed

src/dc_array.rs

Lines changed: 53 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -160,120 +160,81 @@ impl From<Vec<dc_location>> for dc_array_t {
160160
}
161161

162162
pub unsafe fn dc_array_unref(array: *mut dc_array_t) {
163-
if array.is_null() {
164-
return;
165-
}
163+
assert!(!array.is_null());
166164
Box::from_raw(array);
167165
}
168166

169167
pub unsafe fn dc_array_add_uint(array: *mut dc_array_t, item: uintptr_t) {
170-
if !array.is_null() {
171-
(*array).add_uint(item);
172-
}
168+
assert!(!array.is_null());
169+
(*array).add_uint(item);
173170
}
174171

175172
pub unsafe fn dc_array_add_id(array: *mut dc_array_t, item: uint32_t) {
176-
if !array.is_null() {
177-
(*array).add_id(item);
178-
}
173+
assert!(!array.is_null());
174+
(*array).add_id(item);
179175
}
180176

181177
pub unsafe fn dc_array_add_ptr(array: *mut dc_array_t, item: *mut libc::c_void) {
182178
dc_array_add_uint(array, item as uintptr_t);
183179
}
184180

185181
pub unsafe fn dc_array_get_cnt(array: *const dc_array_t) -> size_t {
186-
if array.is_null() {
187-
0
188-
} else {
189-
(*array).len()
190-
}
182+
assert!(!array.is_null());
183+
(*array).len()
191184
}
192185

193186
pub unsafe fn dc_array_get_uint(array: *const dc_array_t, index: size_t) -> uintptr_t {
194-
if array.is_null() || index >= (*array).len() {
195-
0
196-
} else {
197-
(*array).get_uint(index)
198-
}
187+
assert!(!array.is_null());
188+
(*array).get_uint(index)
199189
}
200190

201191
pub unsafe fn dc_array_get_id(array: *const dc_array_t, index: size_t) -> uint32_t {
202-
if array.is_null() || index >= (*array).len() {
203-
0
204-
} else {
205-
(*array).get_id(index)
206-
}
192+
assert!(!array.is_null());
193+
(*array).get_id(index)
207194
}
208195

209196
pub unsafe fn dc_array_get_ptr(array: *const dc_array_t, index: size_t) -> *mut libc::c_void {
210-
if array.is_null() || index >= (*array).len() {
211-
std::ptr::null_mut()
212-
} else {
213-
(*array).get_ptr(index)
214-
}
197+
assert!(!array.is_null());
198+
(*array).get_ptr(index)
215199
}
216200

217201
pub unsafe fn dc_array_get_latitude(array: *const dc_array_t, index: size_t) -> libc::c_double {
218-
if array.is_null() || index >= (*array).len() {
219-
0.0
220-
} else {
221-
(*array).get_latitude(index)
222-
}
202+
assert!(!array.is_null());
203+
(*array).get_latitude(index)
223204
}
224205

225206
pub unsafe fn dc_array_get_longitude(array: *const dc_array_t, index: size_t) -> libc::c_double {
226-
if array.is_null() || index >= (*array).len() {
227-
0.0
228-
} else {
229-
(*array).get_longitude(index)
230-
}
207+
assert!(!array.is_null());
208+
(*array).get_longitude(index)
231209
}
232210

233211
pub unsafe fn dc_array_get_accuracy(array: *const dc_array_t, index: size_t) -> libc::c_double {
234-
if array.is_null() || index >= (*array).len() {
235-
0.0
236-
} else {
237-
(*array).get_accuracy(index)
238-
}
212+
assert!(!array.is_null());
213+
(*array).get_accuracy(index)
239214
}
240215

241216
pub unsafe fn dc_array_get_timestamp(array: *const dc_array_t, index: size_t) -> i64 {
242-
if array.is_null() || index >= (*array).len() {
243-
0
244-
} else {
245-
(*array).get_timestamp(index)
246-
}
217+
assert!(!array.is_null());
218+
(*array).get_timestamp(index)
247219
}
248220

249221
pub unsafe fn dc_array_get_chat_id(array: *const dc_array_t, index: size_t) -> uint32_t {
250-
if array.is_null() || index >= (*array).len() {
251-
0
252-
} else {
253-
(*array).get_chat_id(index)
254-
}
222+
assert!(!array.is_null());
223+
(*array).get_chat_id(index)
255224
}
256225

257226
pub unsafe fn dc_array_get_contact_id(array: *const dc_array_t, index: size_t) -> uint32_t {
258-
if array.is_null() || index >= (*array).len() {
259-
0
260-
} else {
261-
(*array).get_contact_id(index)
262-
}
227+
assert!(!array.is_null());
228+
(*array).get_contact_id(index)
263229
}
264230

265231
pub unsafe fn dc_array_get_msg_id(array: *const dc_array_t, index: size_t) -> uint32_t {
266-
if array.is_null() || index >= (*array).len() {
267-
0
268-
} else {
269-
(*array).get_msg_id(index)
270-
}
232+
assert!(!array.is_null());
233+
(*array).get_msg_id(index)
271234
}
272235

273236
pub unsafe fn dc_array_get_marker(array: *const dc_array_t, index: size_t) -> *mut libc::c_char {
274-
if array.is_null() || index >= (*array).len() {
275-
return std::ptr::null_mut();
276-
}
237+
assert!(!array.is_null());
277238

278239
if let dc_array_t::Locations(v) = &*array {
279240
if let Some(s) = &v[index].marker {
@@ -282,7 +243,7 @@ pub unsafe fn dc_array_get_marker(array: *const dc_array_t, index: size_t) -> *m
282243
std::ptr::null_mut()
283244
}
284245
} else {
285-
std::ptr::null_mut()
246+
panic!("Not an array of locations");
286247
}
287248
}
288249

@@ -297,9 +258,7 @@ pub unsafe fn dc_array_get_marker(array: *const dc_array_t, index: size_t) -> *m
297258
* 1=Location was reported independently.
298259
*/
299260
pub unsafe fn dc_array_is_independent(array: *const dc_array_t, index: size_t) -> libc::c_int {
300-
if array.is_null() || index >= (*array).len() {
301-
return 0;
302-
}
261+
assert!(!array.is_null());
303262

304263
if let dc_array_t::Locations(v) = &*array {
305264
v[index].independent as libc::c_int
@@ -313,9 +272,8 @@ pub unsafe fn dc_array_search_id(
313272
needle: uint32_t,
314273
ret_index: *mut size_t,
315274
) -> bool {
316-
if array.is_null() {
317-
return false;
318-
}
275+
assert!(!array.is_null());
276+
319277
if let Some(i) = (*array).search_id(needle as uintptr_t) {
320278
if !ret_index.is_null() {
321279
*ret_index = i
@@ -327,9 +285,8 @@ pub unsafe fn dc_array_search_id(
327285
}
328286

329287
pub unsafe fn dc_array_get_raw(array: *const dc_array_t) -> *const uintptr_t {
330-
if array.is_null() {
331-
return 0 as *const uintptr_t;
332-
}
288+
assert!(!array.is_null());
289+
333290
if let dc_array_t::Uint(v) = &*array {
334291
v.as_ptr()
335292
} else {
@@ -346,27 +303,24 @@ pub fn dc_array_new_locations(initsize: size_t) -> *mut dc_array_t {
346303
}
347304

348305
pub unsafe fn dc_array_empty(array: *mut dc_array_t) {
349-
if array.is_null() {
350-
return;
351-
}
306+
assert!(!array.is_null());
307+
352308
(*array).clear()
353309
}
354310

355311
pub unsafe fn dc_array_duplicate(array: *const dc_array_t) -> *mut dc_array_t {
356-
if array.is_null() {
357-
std::ptr::null_mut()
358-
} else {
359-
(*array).clone().into_raw()
360-
}
312+
assert!(!array.is_null());
313+
314+
(*array).clone().into_raw()
361315
}
362316

363317
pub unsafe fn dc_array_get_string(
364318
array: *const dc_array_t,
365319
sep: *const libc::c_char,
366320
) -> *mut libc::c_char {
367-
if array.is_null() || sep.is_null() {
368-
return dc_strdup(b"\x00" as *const u8 as *const libc::c_char);
369-
}
321+
assert!(!array.is_null());
322+
assert!(!sep.is_null());
323+
370324
if let dc_array_t::Uint(v) = &*array {
371325
let cnt = v.len();
372326
let sep = as_str(sep);
@@ -412,10 +366,6 @@ mod tests {
412366
);
413367
}
414368

415-
assert_eq!(dc_array_get_id(arr, -1i32 as size_t), 0);
416-
assert_eq!(dc_array_get_id(arr, 1000 as size_t), 0);
417-
assert_eq!(dc_array_get_id(arr, 1001 as size_t), 0);
418-
419369
dc_array_empty(arr);
420370

421371
assert_eq!(dc_array_get_cnt(arr), 0);
@@ -443,4 +393,15 @@ mod tests {
443393
dc_array_unref(arr);
444394
}
445395
}
396+
397+
#[test]
398+
#[should_panic]
399+
fn test_dc_array_out_of_bounds() {
400+
let arr = dc_array_new(7);
401+
for i in 0..1000 {
402+
unsafe { dc_array_add_id(arr, (i + 2) as uint32_t) };
403+
}
404+
unsafe { dc_array_get_id(arr, 1000) };
405+
}
406+
446407
}

0 commit comments

Comments
 (0)