Skip to content
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

TopicPartitionList::elements violates unsafe precondition in slice::from_raw_parts_mut #681

Open
hadronzoo opened this issue May 5, 2024 · 0 comments · Fixed by hadronzoo/rust-rdkafka#1 · May be fixed by #686
Open

Comments

@hadronzoo
Copy link

The following panics on rebalance when compiled with Rust 1.78.0:

impl ConsumerContext for Context {
    fn pre_rebalance(&self, rebalance: &Rebalance) {
        if let Rebalance::Assign(assigned) = rebalance {
            for element in assigned.elements() {
                // never reached
            }
        }
    }
}

Panic message:

unsafe precondition(s) violated: slice::from_raw_parts_mut requires the pointer to be aligned and non-null, and the total size of the slice not to exceed `isize::MAX`

It appears to be caused by the slice::from_raw_parts_mut call in TopicPartitionList::elements:

    pub fn elements(&self) -> Vec<TopicPartitionListElem<'_>> {
        let slice = unsafe { slice::from_raw_parts_mut((*self.ptr).elems, self.count()) };
        let mut vec = Vec::with_capacity(slice.len());
        for elem_ptr in slice {
            vec.push(TopicPartitionListElem::from_ptr(self, &mut *elem_ptr));
        }
        vec
    }

It looks like elems can be null:
image

If I check elements capacity and return if it's 0, this panic does not occur:

            if assigned.capacity() == 0 {
                return;
            }

            for element in assigned.elements() {
                // does not panic

It appears that TopicPartitionList::elements should check that cnt is greater than zero before calling slice::from_raw_parts_mut.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant