{"record":{"id":"506b25518d0522b9","repo":"krahets/hello-algo","slug":"error-506b25","errorCode":null,"errorMessage":"стек пуст","messagePattern":"стек пуст","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"ru/codes/zig/chapter_stack_and_queue/linkedlist_stack.zig","lineNumber":46,"sourceCode":"        // Деструктор (освобождение памяти)\n        pub fn deinit(self: *Self) void {\n            if (self.mem_arena == null) return;\n            self.mem_arena.?.deinit();\n        }\n\n        // Получение длины стека\n        pub fn size(self: *Self) usize {\n            return self.stk_size;\n        }\n\n        // Проверка, пуст ли стек\n        pub fn isEmpty(self: *Self) bool {\n            return self.size() == 0;\n        }\n\n        // Доступ к верхнему элементу стека\n        pub fn peek(self: *Self) T {\n            if (self.size() == 0) @panic(\"стек пуст\");\n            return self.stack_top.?.val;\n        }  \n\n        // Поместить в стек\n        pub fn push(self: *Self, num: T) !void {\n            var node = try self.mem_allocator.create(inc.ListNode(T));\n            node.init(num);\n            node.next = self.stack_top;\n            self.stack_top = node;\n            self.stk_size += 1;\n        } \n\n        // Извлечь из стека\n        pub fn pop(self: *Self) T {\n            var num = self.peek();\n            self.stack_top = self.stack_top.?.next;\n            self.stk_size -= 1;\n            return num;","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/ru/codes/zig/chapter_stack_and_queue/linkedlist_stack.zig#L28-L64","documentation":"Unrecoverable Zig @panic from peek() on the linked-list stack. The guard `if (self.size() == 0) @panic(\"стек пуст\")` runs before `self.stack_top.?.val`, protecting against dereferencing a null top pointer. Peeking the top of an empty stack is defined by this library as a logic error, not a recoverable case.","triggerScenarios":"Call peek() on a LinkedListStack that has never been pushed, or whose top was popped down to null. Also triggered by nested code (evaluators, parsers) that inspects the top before any push or after the final pop.","commonSituations":"Expression evaluators that peek for an operator on an empty operand stack; bracket-matching that peeks before pushing the first bracket; recursive-DFS emulation that peeks the work stack after it empties; tests asserting the top of a freshly built stack.","solutions":["Guard with `if (!stk.isEmpty())` before every peek().","Refactor the algorithm so peek() is only reached inside a branch that already established size() > 0.","Wrap the stack with a peek()-returning-?T adapter if empty-top inspection is normal for your use case."],"exampleFix":"// before\nconst top = stk.peek();\n// after\nconst top = if (stk.isEmpty()) null else stk.peek();","handlingStrategy":"validation","validationCode":"// call BEFORE peek()\nif (stk.isEmpty()) return null;\nconst top = stk.peek();","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Prepend `if (!stk.isEmpty())` to every peek().","Design evaluators/parsers so the top is only peeked after a guaranteed push.","Use a ?T-returning wrapper for stacks where empty-top peeking is normal."],"tags":["zig","stack","peek","data-structure","empty-state"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}