Skip to content

Commit 52546d0

Browse files
hadronzoofede1024
andauthored
fix: check for non-zero count before calling slice::from_raw_parts_mut (#686)
Fixes #681 Co-authored-by: Federico Giraud <fede1024@users.noreply.github.com>
1 parent 5f6a2c5 commit 52546d0

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/topic_partition_list.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,9 @@ impl TopicPartitionList {
317317

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

328331
/// Returns all the elements of the list.
329332
pub fn elements(&self) -> Vec<TopicPartitionListElem<'_>> {
333+
let mut vec = Vec::with_capacity(self.count());
334+
if self.count() == 0 {
335+
return vec;
336+
}
330337
let slice = unsafe { slice::from_raw_parts_mut(self.ptr.elems, self.count()) };
331-
let mut vec = Vec::with_capacity(slice.len());
332338
for elem_ptr in slice {
333339
vec.push(TopicPartitionListElem::from_ptr(self, &mut *elem_ptr));
334340
}
@@ -337,8 +343,11 @@ impl TopicPartitionList {
337343

338344
/// Returns all the elements of the list that belong to the specified topic.
339345
pub fn elements_for_topic<'a>(&'a self, topic: &str) -> Vec<TopicPartitionListElem<'a>> {
346+
let mut vec = Vec::with_capacity(self.count());
347+
if self.count() == 0 {
348+
return vec;
349+
}
340350
let slice = unsafe { slice::from_raw_parts_mut(self.ptr.elems, self.count()) };
341-
let mut vec = Vec::with_capacity(slice.len());
342351
for elem_ptr in slice {
343352
let tp = TopicPartitionListElem::from_ptr(self, &mut *elem_ptr);
344353
if tp.topic() == topic {

0 commit comments

Comments
 (0)