Skip to content

Commit

Permalink
fix: check for non-zero count before calling slice::from_raw_parts_mut (
Browse files Browse the repository at this point in the history
#686)

Fixes #681

Co-authored-by: Federico Giraud <[email protected]>
  • Loading branch information
hadronzoo and fede1024 authored Aug 6, 2024
1 parent 5f6a2c5 commit 52546d0
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/topic_partition_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,9 @@ impl TopicPartitionList {

/// Sets all partitions in the list to the specified offset.
pub fn set_all_offsets(&mut self, offset: Offset) -> Result<(), KafkaError> {
if self.count() == 0 {
return Ok(());
}
let slice = unsafe { slice::from_raw_parts_mut(self.ptr.elems, self.count()) };
for elem_ptr in slice {
let mut elem = TopicPartitionListElem::from_ptr(self, &mut *elem_ptr);
Expand All @@ -327,8 +330,11 @@ impl TopicPartitionList {

/// Returns all the elements of the list.
pub fn elements(&self) -> Vec<TopicPartitionListElem<'_>> {
let mut vec = Vec::with_capacity(self.count());
if self.count() == 0 {
return vec;
}
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));
}
Expand All @@ -337,8 +343,11 @@ impl TopicPartitionList {

/// Returns all the elements of the list that belong to the specified topic.
pub fn elements_for_topic<'a>(&'a self, topic: &str) -> Vec<TopicPartitionListElem<'a>> {
let mut vec = Vec::with_capacity(self.count());
if self.count() == 0 {
return vec;
}
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 {
let tp = TopicPartitionListElem::from_ptr(self, &mut *elem_ptr);
if tp.topic() == topic {
Expand Down

0 comments on commit 52546d0

Please sign in to comment.