A weird crash case (debug print line affects code behavior)

Here is a piece code in my zig project:

fn getFreeNode(self: *@This()) !*Node {
    const n = if (self.tryToGetFreeNode()) |node| node else try self.allocator.create(Node);

std.debug.print("==== 111 =====\n", .{});

    n.* = .{
        .value = .{},
std.debug.print("==== 222 =====\n", .{});


    std.debug.assert(n.value.textSegment.len == 0);
    std.debug.assert(n.value.deeperTree.count == 0);

    return n;

I haven’t found crash cases when building it in Debug mode.
When I use the -OReleaseSafe mode, I also haven’t found crash cases when the 222 print line is enabled. But when the 222 line is disabled, a crash case is found as:

===== 111 =====
thread 251220 panic: reached unreachable code

However, I don’t think there is any unreachable code between the 111 and 222 print lines.

I am looking into this issue. But if anyone can shed some light on this, I would very appreciate it.

Okay, I have found the cause. It is because n.value.textSegment is not initialized.

But here, the effect of the 222 debug print line is really some misleading.

undefined as a default value for a field is typically a bad idea. If you haven’t already, I’d recommend reading the docs for undefined.

In the case of a slice, you likely want to initialize it to an empty slice (= &.{} will work), or make it optional and initialize it to null.


I see. But here I deliberately keep it uninitialized and made a mistake to use it before initializing. It is just some weird the problem is exposed at certain unexpected conditions.

@zigo Please edit your question to replace the screenshot of code, with actual text, you can copy your source code text and format it as code with:

code here

Screenshots of code are only appropriate if the syntax highlighting is broken and needs to be discussed/fixed, otherwise it is unwanted, because it makes the code unsearchable and prevents people that need assistive technologies like screen readers from reading it.

Additionally it is also annoying, because it can’t be copy pasted so that it can be turned into an example and executed.