| ... | @@ -83,11 +83,17 @@ pub const AccumBuffer = struct { | ... | @@ -83,11 +83,17 @@ pub const AccumBuffer = struct { |
| 83 | return error.CorruptInput; | 83 | return error.CorruptInput; |
| 84 | } | 84 | } |
| 85 | | 85 | |
| | 86 | // ensure we have enough capacity for all new bytes. |
| | 87 | // This prevents the buffer's memory from being reallocated (and freed) |
| | 88 | // while we are still reading from it. |
| | 89 | try self.buf.ensureTotalCapacity(allocator, buf_len + len); |
| | 90 | |
| 86 | var offset = buf_len - dist; | 91 | var offset = buf_len - dist; |
| 87 | var i: usize = 0; | 92 | var i: usize = 0; |
| 88 | while (i < len) : (i += 1) { | 93 | while (i < len) : (i += 1) { |
| 89 | const x = self.buf.items[offset]; | 94 | const x = self.buf.items[offset]; |
| 90 | try self.buf.append(allocator, x); | 95 | // Since capacity is guaranteed, it's safe to use appendAssumeCapacity. |
| | 96 | self.buf.appendAssumeCapacity(x); |
| 91 | offset += 1; | 97 | offset += 1; |
| 92 | } | 98 | } |
| 93 | self.len += len; | 99 | self.len += len; |