-
Notifications
You must be signed in to change notification settings - Fork 4k
ARROW-7836: [Rust] "allocate_aligned"/"reallocate" need to initialize memory to avoid UB #6422
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
Conversation
|
The CI failures are unrelated. @Marwes @jturner314 are you happy with this solution? |
jturner314
left a comment
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.
With the addition of a null pointer check (see the comment), this looks good to me. I think zeroing out all new memory, as in this PR, is the safest option. My understanding is that zeroing memory is very fast. At some later point, it may be worth revisiting this to see how much of a performance impact it has. (For example, it may be sufficient to just zero out the padding used for SIMD, instead of the entire allocation.) But, without a more thorough review of accesses into memory buffers, I'd prioritize safety over potential performance gains.
rust/arrow/src/memory.rs
Outdated
| ); | ||
| if new_size > old_size { | ||
| std::ptr::write_bytes( | ||
| new_ptr.offset(old_size as isize), |
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.
Fwiw, using the add method is equivalent and a bit more concise:
| new_ptr.offset(old_size as isize), | |
| new_ptr.add(old_size), |
rust/arrow/src/memory.rs
Outdated
| new_size, | ||
| ) | ||
| ); | ||
| if new_size > old_size { |
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.
There should be an additional check here in case realloc returns a null pointer (indicating an error), since otherwise the offset will be out-of-bounds. (Also, std::ptr::write_bytes requires the pointer to be non-null.)
| if new_size > old_size { | |
| if !new_ptr.is_null() && new_size > old_size { |
rust/arrow/src/memory.rs
Outdated
| ) | ||
| ); | ||
| if new_size > old_size { | ||
| std::ptr::write_bytes( |
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.
Fwiw, using the .write_bytes() method on the pointer would be a bit more concise.
|
I addressed all the comments. @jturner314 thank you very much for all your time and help!
Yea, definitely my thinking too. |
|
@andygrove, @nevi-me @sunchao can I get at least one approval from another committer to get this merged if you have a chance? |
No description provided.