File tree Expand file tree Collapse file tree 1 file changed +10
-6
lines changed Expand file tree Collapse file tree 1 file changed +10
-6
lines changed Original file line number Diff line number Diff line change @@ -100,8 +100,10 @@ impl Buffer {
100
100
/// This method panics if `size` overflows `isize::MAX`.
101
101
#[ inline]
102
102
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) ;
105
107
}
106
108
107
109
/// Same as String::reserve except that undefined behaviour can result if `size`
@@ -135,11 +137,13 @@ impl Buffer {
135
137
#[ inline]
136
138
pub fn push_str ( & mut self , data : & str ) {
137
139
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) ;
138
146
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) ;
143
147
let p = self . data . add ( self . len ) ;
144
148
std:: ptr:: copy_nonoverlapping ( data. as_ptr ( ) , p, size) ;
145
149
self . len += size;
You can’t perform that action at this time.
0 commit comments