Decompressing LZMA Data - @memcpy arguments alias error

Environment:
OS: Manjaro Linux
Zig: 0.13.0

I’m trying to decompress an LZMA-compressed binary data file using Zig’s std.compress.lzma. Here’s the code I’m working with:

const std = @import("std");

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer _ = gpa.deinit();

    const allocator = gpa.allocator();

    // Open compressed file for reading
    const file_path = "./01h_ticks";
    var file = try std.fs.cwd().openFile(file_path, .{});
    defer file.close();

    // Read compressed file into memory
    const compressed_data = try allocator.alloc(u8, 20_000);
    defer allocator.free(compressed_data);

    _ = try file.readAll(compressed_data);

    // Wrap the compressed data in a FixedBufferStream
    var compressed_stream = std.io.fixedBufferStream(compressed_data);
    const compressed_reader = compressed_stream.reader();

    // Create a decompression stream
    var decompress_stream = try std.compress.lzma.decompress(allocator, compressed_reader);
    defer decompress_stream.deinit();
    const decompress_stream_reader = decompress_stream.reader();

    // Allocate a buffer for decompressed data
    var temp_buffer: []u8 = try allocator.alloc(u8, 20_000);
    defer allocator.free(temp_buffer);

    // Read decompressed data
    _ = try decompress_stream_reader.read(temp_buffer[0..]);
}

However, I run into the following runtime error when calling read() on the decompress_stream_reader:

thread 1436544 panic: @memcpy arguments alias
/usr/lib/zig/std/compress/lzma.zig:80:53: 0x103cc58 in read (main)
            @memcpy(input[0 .. input.len - n], input[n..]);
                                                    ^
/usr/lib/zig/std/io.zig:94:26: 0x103b95b in main (main)
            return readFn(self.context, buffer);
                         ^
/usr/lib/zig/std/start.zig:524:37: 0x1038f25 in posixCallMainAndExit (main)
            const result = root.main() catch |err| {
                                    ^
/usr/lib/zig/std/start.zig:266:5: 0x1038a41 in _start (main)
    asm volatile (switch (native_arch) {
    ^
???:?:?: 0x0 in ??? (???)
[1]    1436544 IOT instruction (core dumped)

Does anyone have experience working with std.compress.lzma and know what might be causing this? Any help would be greatly appreciated.

Thanks in advance.

I have no error.
It finished successfully.

Environment:

OS: MacOS ventura
Zig: 0.14.0-dev.2126+e27b4647d

1 Like

That part of the code in lzma.zig seems to just be incorrect. If n != input.len then input[0 .. input.len - n] will always overlap with input[n..] and trigger the alias safety check panic that you are observing.

2 Likes

Thank you, everyone. The issue has been resolved by updating to Zig 0.14.0-dev.2487+af89bb05d and increasing the buffer size (temp_buffer) used for storing the decompressed data. I really appreciate the patience.

1 Like