-
-
Notifications
You must be signed in to change notification settings - Fork 117
dc_array: panic on null pointers and out of range indexes #333
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -160,120 +160,81 @@ impl From<Vec<dc_location>> for dc_array_t { | |
| } | ||
|
|
||
| pub unsafe fn dc_array_unref(array: *mut dc_array_t) { | ||
| if array.is_null() { | ||
| return; | ||
| } | ||
| assert!(!array.is_null()); | ||
| Box::from_raw(array); | ||
| } | ||
|
|
||
| pub unsafe fn dc_array_add_uint(array: *mut dc_array_t, item: uintptr_t) { | ||
| if !array.is_null() { | ||
| (*array).add_uint(item); | ||
| } | ||
| assert!(!array.is_null()); | ||
| (*array).add_uint(item); | ||
| } | ||
|
|
||
| pub unsafe fn dc_array_add_id(array: *mut dc_array_t, item: uint32_t) { | ||
| if !array.is_null() { | ||
| (*array).add_id(item); | ||
| } | ||
| assert!(!array.is_null()); | ||
| (*array).add_id(item); | ||
| } | ||
|
|
||
| pub unsafe fn dc_array_add_ptr(array: *mut dc_array_t, item: *mut libc::c_void) { | ||
| dc_array_add_uint(array, item as uintptr_t); | ||
| } | ||
|
|
||
| pub unsafe fn dc_array_get_cnt(array: *const dc_array_t) -> size_t { | ||
| if array.is_null() { | ||
| 0 | ||
| } else { | ||
| (*array).len() | ||
| } | ||
| assert!(!array.is_null()); | ||
| (*array).len() | ||
| } | ||
|
|
||
| pub unsafe fn dc_array_get_uint(array: *const dc_array_t, index: size_t) -> uintptr_t { | ||
| if array.is_null() || index >= (*array).len() { | ||
| 0 | ||
| } else { | ||
| (*array).get_uint(index) | ||
| } | ||
| assert!(!array.is_null()); | ||
| (*array).get_uint(index) | ||
| } | ||
|
|
||
| pub unsafe fn dc_array_get_id(array: *const dc_array_t, index: size_t) -> uint32_t { | ||
| if array.is_null() || index >= (*array).len() { | ||
| 0 | ||
| } else { | ||
| (*array).get_id(index) | ||
| } | ||
| assert!(!array.is_null()); | ||
| (*array).get_id(index) | ||
| } | ||
|
|
||
| pub unsafe fn dc_array_get_ptr(array: *const dc_array_t, index: size_t) -> *mut libc::c_void { | ||
| if array.is_null() || index >= (*array).len() { | ||
| std::ptr::null_mut() | ||
| } else { | ||
| (*array).get_ptr(index) | ||
| } | ||
| assert!(!array.is_null()); | ||
| (*array).get_ptr(index) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here you remove
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| pub unsafe fn dc_array_get_latitude(array: *const dc_array_t, index: size_t) -> libc::c_double { | ||
| if array.is_null() || index >= (*array).len() { | ||
| 0.0 | ||
| } else { | ||
| (*array).get_latitude(index) | ||
| } | ||
| assert!(!array.is_null()); | ||
| (*array).get_latitude(index) | ||
| } | ||
|
|
||
| pub unsafe fn dc_array_get_longitude(array: *const dc_array_t, index: size_t) -> libc::c_double { | ||
| if array.is_null() || index >= (*array).len() { | ||
| 0.0 | ||
| } else { | ||
| (*array).get_longitude(index) | ||
| } | ||
| assert!(!array.is_null()); | ||
| (*array).get_longitude(index) | ||
| } | ||
|
|
||
| pub unsafe fn dc_array_get_accuracy(array: *const dc_array_t, index: size_t) -> libc::c_double { | ||
| if array.is_null() || index >= (*array).len() { | ||
| 0.0 | ||
| } else { | ||
| (*array).get_accuracy(index) | ||
| } | ||
| assert!(!array.is_null()); | ||
| (*array).get_accuracy(index) | ||
| } | ||
|
|
||
| pub unsafe fn dc_array_get_timestamp(array: *const dc_array_t, index: size_t) -> i64 { | ||
| if array.is_null() || index >= (*array).len() { | ||
| 0 | ||
| } else { | ||
| (*array).get_timestamp(index) | ||
| } | ||
| assert!(!array.is_null()); | ||
| (*array).get_timestamp(index) | ||
| } | ||
|
|
||
| pub unsafe fn dc_array_get_chat_id(array: *const dc_array_t, index: size_t) -> uint32_t { | ||
| if array.is_null() || index >= (*array).len() { | ||
| 0 | ||
| } else { | ||
| (*array).get_chat_id(index) | ||
| } | ||
| assert!(!array.is_null()); | ||
| (*array).get_chat_id(index) | ||
| } | ||
|
|
||
| pub unsafe fn dc_array_get_contact_id(array: *const dc_array_t, index: size_t) -> uint32_t { | ||
| if array.is_null() || index >= (*array).len() { | ||
| 0 | ||
| } else { | ||
| (*array).get_contact_id(index) | ||
| } | ||
| assert!(!array.is_null()); | ||
| (*array).get_contact_id(index) | ||
| } | ||
|
|
||
| pub unsafe fn dc_array_get_msg_id(array: *const dc_array_t, index: size_t) -> uint32_t { | ||
| if array.is_null() || index >= (*array).len() { | ||
| 0 | ||
| } else { | ||
| (*array).get_msg_id(index) | ||
| } | ||
| assert!(!array.is_null()); | ||
| (*array).get_msg_id(index) | ||
| } | ||
|
|
||
| pub unsafe fn dc_array_get_marker(array: *const dc_array_t, index: size_t) -> *mut libc::c_char { | ||
| if array.is_null() || index >= (*array).len() { | ||
| return std::ptr::null_mut(); | ||
| } | ||
| assert!(!array.is_null()); | ||
|
|
||
| if let dc_array_t::Locations(v) = &*array { | ||
| 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 | |
| std::ptr::null_mut() | ||
| } | ||
| } else { | ||
| std::ptr::null_mut() | ||
| panic!("Not an array of locations"); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -297,9 +258,7 @@ pub unsafe fn dc_array_get_marker(array: *const dc_array_t, index: size_t) -> *m | |
| * 1=Location was reported independently. | ||
| */ | ||
| pub unsafe fn dc_array_is_independent(array: *const dc_array_t, index: size_t) -> libc::c_int { | ||
| if array.is_null() || index >= (*array).len() { | ||
| return 0; | ||
| } | ||
| assert!(!array.is_null()); | ||
|
|
||
| if let dc_array_t::Locations(v) = &*array { | ||
| v[index].independent as libc::c_int | ||
|
|
@@ -313,9 +272,8 @@ pub unsafe fn dc_array_search_id( | |
| needle: uint32_t, | ||
| ret_index: *mut size_t, | ||
| ) -> bool { | ||
| if array.is_null() { | ||
| return false; | ||
| } | ||
| assert!(!array.is_null()); | ||
|
|
||
| if let Some(i) = (*array).search_id(needle as uintptr_t) { | ||
| if !ret_index.is_null() { | ||
| *ret_index = i | ||
|
|
@@ -327,9 +285,8 @@ pub unsafe fn dc_array_search_id( | |
| } | ||
|
|
||
| pub unsafe fn dc_array_get_raw(array: *const dc_array_t) -> *const uintptr_t { | ||
| if array.is_null() { | ||
| return 0 as *const uintptr_t; | ||
| } | ||
| assert!(!array.is_null()); | ||
|
|
||
| if let dc_array_t::Uint(v) = &*array { | ||
| v.as_ptr() | ||
| } else { | ||
|
|
@@ -346,27 +303,24 @@ pub fn dc_array_new_locations(initsize: size_t) -> *mut dc_array_t { | |
| } | ||
|
|
||
| pub unsafe fn dc_array_empty(array: *mut dc_array_t) { | ||
| if array.is_null() { | ||
| return; | ||
| } | ||
| assert!(!array.is_null()); | ||
|
|
||
| (*array).clear() | ||
| } | ||
|
|
||
| pub unsafe fn dc_array_duplicate(array: *const dc_array_t) -> *mut dc_array_t { | ||
| if array.is_null() { | ||
| std::ptr::null_mut() | ||
| } else { | ||
| (*array).clone().into_raw() | ||
| } | ||
| assert!(!array.is_null()); | ||
|
|
||
| (*array).clone().into_raw() | ||
| } | ||
|
|
||
| pub unsafe fn dc_array_get_string( | ||
| array: *const dc_array_t, | ||
| sep: *const libc::c_char, | ||
| ) -> *mut libc::c_char { | ||
| if array.is_null() || sep.is_null() { | ||
| return dc_strdup(b"\x00" as *const u8 as *const libc::c_char); | ||
| } | ||
| assert!(!array.is_null()); | ||
| assert!(!sep.is_null()); | ||
|
|
||
| if let dc_array_t::Uint(v) = &*array { | ||
| let cnt = v.len(); | ||
| let sep = as_str(sep); | ||
|
|
@@ -412,10 +366,6 @@ mod tests { | |
| ); | ||
| } | ||
|
|
||
| assert_eq!(dc_array_get_id(arr, -1i32 as size_t), 0); | ||
| assert_eq!(dc_array_get_id(arr, 1000 as size_t), 0); | ||
| assert_eq!(dc_array_get_id(arr, 1001 as size_t), 0); | ||
|
|
||
| dc_array_empty(arr); | ||
|
|
||
| assert_eq!(dc_array_get_cnt(arr), 0); | ||
|
|
@@ -443,4 +393,15 @@ mod tests { | |
| dc_array_unref(arr); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| #[should_panic] | ||
| fn test_dc_array_out_of_bounds() { | ||
| let arr = dc_array_new(7); | ||
| for i in 0..1000 { | ||
| unsafe { dc_array_add_id(arr, (i + 2) as uint32_t) }; | ||
| } | ||
| unsafe { dc_array_get_id(arr, 1000) }; | ||
| } | ||
|
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Panicing on a null pointer changes the behaviour of the ffi, i think we should avoid this. If it's null, we should return a
0https://github.com/deltachat/deltachat-core/blob/e4ce281dc2c8cb1d0765f0e2e73f7846cbba3faa/src/dc_array.c#L252
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@r10s what do you think about this? Is it likely that we break android/ios at some points with this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #291 for previous discussion of similar changes. No input from @r10s there, but it got merged.