@@ -160,120 +160,81 @@ impl From<Vec<dc_location>> for dc_array_t {
160
160
}
161
161
162
162
pub unsafe fn dc_array_unref ( array : * mut dc_array_t ) {
163
- if array. is_null ( ) {
164
- return ;
165
- }
163
+ assert ! ( !array. is_null( ) ) ;
166
164
Box :: from_raw ( array) ;
167
165
}
168
166
169
167
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) ;
173
170
}
174
171
175
172
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) ;
179
175
}
180
176
181
177
pub unsafe fn dc_array_add_ptr ( array : * mut dc_array_t , item : * mut libc:: c_void ) {
182
178
dc_array_add_uint ( array, item as uintptr_t ) ;
183
179
}
184
180
185
181
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 ( )
191
184
}
192
185
193
186
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)
199
189
}
200
190
201
191
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)
207
194
}
208
195
209
196
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)
215
199
}
216
200
217
201
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)
223
204
}
224
205
225
206
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)
231
209
}
232
210
233
211
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)
239
214
}
240
215
241
216
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)
247
219
}
248
220
249
221
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)
255
224
}
256
225
257
226
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)
263
229
}
264
230
265
231
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)
271
234
}
272
235
273
236
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( ) ) ;
277
238
278
239
if let dc_array_t:: Locations ( v) = & * array {
279
240
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
282
243
std:: ptr:: null_mut ( )
283
244
}
284
245
} else {
285
- std :: ptr :: null_mut ( )
246
+ panic ! ( "Not an array of locations" ) ;
286
247
}
287
248
}
288
249
@@ -297,9 +258,7 @@ pub unsafe fn dc_array_get_marker(array: *const dc_array_t, index: size_t) -> *m
297
258
* 1=Location was reported independently.
298
259
*/
299
260
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( ) ) ;
303
262
304
263
if let dc_array_t:: Locations ( v) = & * array {
305
264
v[ index] . independent as libc:: c_int
@@ -313,9 +272,8 @@ pub unsafe fn dc_array_search_id(
313
272
needle : uint32_t ,
314
273
ret_index : * mut size_t ,
315
274
) -> bool {
316
- if array. is_null ( ) {
317
- return false ;
318
- }
275
+ assert ! ( !array. is_null( ) ) ;
276
+
319
277
if let Some ( i) = ( * array) . search_id ( needle as uintptr_t ) {
320
278
if !ret_index. is_null ( ) {
321
279
* ret_index = i
@@ -327,9 +285,8 @@ pub unsafe fn dc_array_search_id(
327
285
}
328
286
329
287
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
+
333
290
if let dc_array_t:: Uint ( v) = & * array {
334
291
v. as_ptr ( )
335
292
} else {
@@ -346,27 +303,24 @@ pub fn dc_array_new_locations(initsize: size_t) -> *mut dc_array_t {
346
303
}
347
304
348
305
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
+
352
308
( * array) . clear ( )
353
309
}
354
310
355
311
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 ( )
361
315
}
362
316
363
317
pub unsafe fn dc_array_get_string (
364
318
array : * const dc_array_t ,
365
319
sep : * const libc:: c_char ,
366
320
) -> * 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
+
370
324
if let dc_array_t:: Uint ( v) = & * array {
371
325
let cnt = v. len ( ) ;
372
326
let sep = as_str ( sep) ;
@@ -412,10 +366,6 @@ mod tests {
412
366
) ;
413
367
}
414
368
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
-
419
369
dc_array_empty ( arr) ;
420
370
421
371
assert_eq ! ( dc_array_get_cnt( arr) , 0 ) ;
@@ -443,4 +393,15 @@ mod tests {
443
393
dc_array_unref ( arr) ;
444
394
}
445
395
}
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
+
446
407
}
0 commit comments