Commit 00b0beb682

binarycraft007 <elliot.huang.signed@gmail.com>
2025-09-01 11:01:54
lzma2: fix array list looping logic in appendLz
1 parent bc79553
Changed files (1)
lib
std
compress
lib/std/compress/lzma2.zig
@@ -83,11 +83,17 @@ pub const AccumBuffer = struct {
             return error.CorruptInput;
         }
 
+        // ensure we have enough capacity for all new bytes.
+        // This prevents the buffer's memory from being reallocated (and freed)
+        // while we are still reading from it.
+        try self.buf.ensureTotalCapacity(allocator, buf_len + len);
+
         var offset = buf_len - dist;
         var i: usize = 0;
         while (i < len) : (i += 1) {
             const x = self.buf.items[offset];
-            try self.buf.append(allocator, x);
+            // Since capacity is guaranteed, it's safe to use appendAssumeCapacity.
+            self.buf.appendAssumeCapacity(x);
             offset += 1;
         }
         self.len += len;