commit d51d18c98654e09cb1af7f2896f97b81ce6c83bb (tree) parent f872dd03da64e5b53d3a5c7a95344b5b64a7aacb Author: Andrew Kelley <andrew@ziglang.org> Date: Tue, 2 Sep 2025 13:16:09 -0700 Merge pull request #25105 from binarycraft007/lzma2-fix lzma2: fix array list looping logic in appendLz Diffstat:
| M | lib/std/compress/lzma2.zig | | | 21 | +++++++++++---------- |
1 file changed, 11 insertions(+), 10 deletions(-)
diff --git a/lib/std/compress/lzma2.zig b/lib/std/compress/lzma2.zig @@ -79,17 +79,18 @@ pub const AccumBuffer = struct { _ = writer; const buf_len = self.buf.items.len; - if (dist > buf_len) { - return error.CorruptInput; - } + if (dist > buf_len) return error.CorruptInput; - 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); - offset += 1; - } + try self.buf.ensureUnusedCapacity(allocator, len); + const buffer = self.buf.allocatedSlice(); + const src = buffer[buf_len - dist ..][0..len]; + const dst = buffer[buf_len..][0..len]; + + // This is not a @memmove; it intentionally repeats patterns caused by + // iterating one byte at a time. + for (dst, src) |*d, s| d.* = s; + + self.buf.items.len = buf_len + len; self.len += len; }