#segfault while trying to print

1 messages · Page 1 of 1 (latest)

pine dawn
#

I somehow can't print whatever I'm returning?
not sure if I'm straight up doing something wrong or I overlooked something

code below:

#
const std = @import("std");

pub fn distroName(allocator: std.mem.Allocator) ![]const u8 {
    const file = try std.fs.openFileAbsolute("/etc/os-release", .{});
    defer file.close();

    var buf = std.ArrayList(u8).init(allocator);
    defer buf.deinit();
    _ = try file.reader().readAllArrayList(&buf, 60000);

    var distro: []const u8 = "Some distro";

    var window = std.mem.split(u8, buf.items, "\n");
    while (window.next()) |line| {
        if (std.mem.containsAtLeast(u8, line, 1, "PRETTY_NAME")) {
            const l = std.mem.trimLeft(u8, line, "PRETTY_NAME=\"");
            const r = std.mem.trimRight(u8, l, "\"");
            distro = r;
            std.debug.print("{s}\n", .{r});
        }
    }

    return distro;
}

test "distro" {
    const d = try distroName(std.testing.allocator);
    std.debug.print("{s}\n", .{d});
}
#

doesn't work either if i immediately return r from the check

#

it does print properly when inside of the function

#

printing distro inside the function works as well

#

stacktrace

thread 25035 panic: reached unreachable code
/usr/lib/zig/std/posix.zig:1223:23: 0x1082b1e in write (test)
            .FAULT => unreachable,
                      ^
/usr/lib/zig/std/fs/File.zig:1281:23: 0x104a92a in write (test)
    return posix.write(self.handle, bytes);
                      ^
/usr/lib/zig/std/io.zig:360:27: 0x1044a0a in typeErasedWriteFn (test)
            return writeFn(ptr.*, bytes);
                          ^
/usr/lib/zig/std/io/Writer.zig:13:24: 0x10949cb in write (test)
    return self.writeFn(self.context, bytes);
                       ^
/usr/lib/zig/std/io/Writer.zig:19:32: 0x1074a13 in writeAll (test)
        index += try self.write(bytes[index..]);
                               ^
/usr/lib/zig/std/fmt.zig:1043:28: 0x109656d in formatBuf__anon_9410 (test)
        try writer.writeAll(buf);
                           ^
/usr/lib/zig/std/fmt.zig:637:37: 0x1082da9 in formatType__anon_8879 (test)
                    return formatBuf(value, options, writer);
                                    ^
/usr/lib/zig/std/fmt.zig:185:23: 0x104a9b6 in format__anon_5333 (test)
        try formatType(
                      ^
/usr/lib/zig/std/io/Writer.zig:24:26: 0x1044a90 in print__anon_3824 (test)
    return std.fmt.format(self, format, args);
                         ^
/usr/lib/zig/std/io.zig:324:47: 0x103e410 in print__anon_3513 (test)
            return @errorCast(self.any().print(format, args));
                                              ^
/home/koton-bads/Documents/Zig/clay fetch/src/fetch.zig:29:20: 0x103e584 in test.distro (test)
    std.debug.print("{s}\n", .{d});
                   ^
/usr/lib/zig/compiler/test_runner.zig:95:29: 0x1050063 in mainServer (test)
                test_fn.func() catch |err| switch (err) {
                            ^
/usr/lib/zig/compiler/test_runner.zig:35:26: 0x104531e in main (test)
        return mainServer() catch @panic("internal test runner failure");
                         ^
/usr/lib/zig/std/start.zig:514:22: 0x10406f9 in posixCallMainAndExit (test)
            root.main();
                     ^
/usr/lib/zig/std/start.zig:266:5: 0x1040261 in _start (test)
    asm volatile (switch (native_arch) {
    ^
???:?:?: 0x1 in ??? (???)
Unwind information for `???:0x1` was not available, trace may be incomplete```
fleet narwhal
#

you can't print it after you already free the buf ArrayList

#

maybe do something like return allocator.dupe(u8, distro) and then let the caller to free the memory

pine dawn
fleet narwhal
#

Instead of using an ArrayList to read a file (which is not really ideal), try something like this.

const buffer = try allocator.alloc(u8, try file.getEndPos());
defer allocator.free(buffer);

_ = try file.readAll(buffer);
fleet narwhal