Skip to content

Commit 2477663

Browse files
committed
glib: Avoid more integer overflows when reserving space in PtrSlice / Slice / StrV
1 parent 485020b commit 2477663

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

glib/src/collections/ptr_slice.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ impl<T: TransparentPtrType> PtrSlice<T> {
713713
#[allow(clippy::int_plus_one)]
714714
pub fn reserve(&mut self, additional: usize) {
715715
// Nothing new to reserve as there's still enough space
716-
if self.len + additional + 1 <= self.capacity {
716+
if additional < self.capacity - self.len {
717717
return;
718718
}
719719

@@ -788,7 +788,7 @@ impl<T: TransparentPtrType> PtrSlice<T> {
788788
#[inline]
789789
pub fn extend_from_slice(&mut self, other: &[T]) {
790790
// Nothing new to reserve as there's still enough space
791-
if self.len + other.len() + 1 > self.capacity {
791+
if other.len() >= self.capacity - self.len {
792792
self.reserve(other.len());
793793
}
794794

@@ -815,7 +815,7 @@ impl<T: TransparentPtrType> PtrSlice<T> {
815815
assert!(index <= self.len);
816816

817817
// Nothing new to reserve as there's still enough space
818-
if self.len + 1 + 1 > self.capacity {
818+
if 1 >= self.capacity - self.len {
819819
self.reserve(1);
820820
}
821821

@@ -842,7 +842,7 @@ impl<T: TransparentPtrType> PtrSlice<T> {
842842
#[inline]
843843
pub fn push(&mut self, item: T) {
844844
// Nothing new to reserve as there's still enough space
845-
if self.len + 1 + 1 > self.capacity {
845+
if 1 >= self.capacity - self.len {
846846
self.reserve(1);
847847
}
848848

glib/src/collections/slice.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ impl<T: TransparentType> Slice<T> {
614614
/// Reserves at least this much additional capacity.
615615
pub fn reserve(&mut self, additional: usize) {
616616
// Nothing new to reserve as there's still enough space
617-
if self.len + additional <= self.capacity {
617+
if additional <= self.capacity - self.len {
618618
return;
619619
}
620620

@@ -685,7 +685,7 @@ impl<T: TransparentType> Slice<T> {
685685
#[inline]
686686
pub fn extend_from_slice(&mut self, other: &[T]) {
687687
// Nothing new to reserve as there's still enough space
688-
if self.len + other.len() > self.capacity {
688+
if other.len() > self.capacity - self.len {
689689
self.reserve(other.len());
690690
}
691691

@@ -706,7 +706,7 @@ impl<T: TransparentType> Slice<T> {
706706
assert!(index <= self.len);
707707

708708
// Nothing new to reserve as there's still enough space
709-
if self.len + 1 > self.capacity {
709+
if 1 > self.capacity - self.len {
710710
self.reserve(1);
711711
}
712712

@@ -729,7 +729,7 @@ impl<T: TransparentType> Slice<T> {
729729
#[allow(clippy::int_plus_one)]
730730
pub fn push(&mut self, item: T) {
731731
// Nothing new to reserve as there's still enough space
732-
if self.len + 1 > self.capacity {
732+
if 1 > self.capacity - self.len {
733733
self.reserve(1);
734734
}
735735

glib/src/collections/strv.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,7 @@ impl StrV {
800800
assert!(index <= self.len);
801801

802802
// Nothing new to reserve as there's still enough space
803-
if self.len + 1 + 1 > self.capacity {
803+
if 1 >= self.capacity - self.len {
804804
self.reserve(1);
805805
}
806806

@@ -824,7 +824,7 @@ impl StrV {
824824
#[inline]
825825
pub fn push(&mut self, item: GString) {
826826
// Nothing new to reserve as there's still enough space
827-
if self.len + 1 + 1 > self.capacity {
827+
if 1 >= self.capacity - self.len {
828828
self.reserve(1);
829829
}
830830

0 commit comments

Comments
 (0)