Skip to content

Crash when node not found in singly linked list #25304

@cpranga

Description

@cpranga

Zig Version

0.16.0-dev.225+0b75a2a1b

Steps to Reproduce and Observed Behavior

const std = @import("std");
const SinglyLinkedList = std.SinglyLinkedList;

pub fn main() !void {
    const List = struct {
        node: SinglyLinkedList.Node = .{},
    };
    var list: SinglyLinkedList = .{};

    var one: List = .{};
    var two: List = .{};

    list.prepend(&one.node);
    one.node.insertAfter(&two.node);
    _ = list.remove(&two.node);
    _ = list.remove(&two.node);
}
havelock@pop-os:~/code/scr$ zig run sll_error.zig 
thread 134888 panic: attempt to use null value
/home/havelock/bin/zig/lib/std/SinglyLinkedList.zig:94:43: 0x1137b3b in remove (std.zig)
            current_elm = current_elm.next.?;
                                          ^
/home/havelock/code/scr/sll_error.zig:16:20: 0x11364ff in main (sll_error.zig)
    _ = list.remove(&two.node);
                   ^
/home/havelock/bin/zig/lib/std/start.zig:627:37: 0x1136cad in posixCallMainAndExit (std.zig)
            const result = root.main() catch |err| {
                                    ^
/home/havelock/bin/zig/lib/std/start.zig:232:5: 0x1136421 in _start (std.zig)
    asm volatile (switch (native_arch) {
    ^
???:?:?: 0x0 in ??? (???)
Aborted (core dumped)

Expected Behavior

I would expect the function to return false when the node is not removed, or at the very least avoid crashing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions