Skip to content

Commit 5961a74

Browse files
committed
security: Properly handle slices with size greater than isize::MAX
See rust-lang/rust#79930 (comment) for more details.
1 parent 5108988 commit 5961a74

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

sailfish/src/runtime/buffer.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,10 @@ impl Buffer {
100100
/// This method panics if `size` overflows `isize::MAX`.
101101
#[inline]
102102
pub fn reserve(&mut self, size: usize) {
103-
assert!(size <= std::isize::MAX as usize);
104-
unsafe { self.reserve_small(size) };
103+
if size <= self.capacity - self.len {
104+
return;
105+
}
106+
self.reserve_internal(size);
105107
}
106108

107109
/// Same as String::reserve except that undefined behaviour can result if `size`
@@ -135,11 +137,13 @@ impl Buffer {
135137
#[inline]
136138
pub fn push_str(&mut self, data: &str) {
137139
let size = data.len();
140+
141+
// NOTE: Since there's no guarantee that the maximum slice size won't overflow
142+
// isize::MAX, we must call `reserve()` instead of `reserve_small()`. See
143+
// https://github.com/rust-lang/rust/pull/79930#issuecomment-747135498 for more
144+
// details.
145+
self.reserve(size);
138146
unsafe {
139-
// SAFETY: slice length is in general limited to isize::MAX bytes.
140-
// See https://github.com/rust-lang/rust/pull/79930#issuecomment-745155197
141-
// for details.
142-
self.reserve_small(size);
143147
let p = self.data.add(self.len);
144148
std::ptr::copy_nonoverlapping(data.as_ptr(), p, size);
145149
self.len += size;

0 commit comments

Comments
 (0)