{"record":{"id":"3313e76a9be0e2d6","repo":"krahets/hello-algo","slug":"error-3313e7","errorCode":null,"errorMessage":"索引越界","messagePattern":"索引越界","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"codes/zig/chapter_array_and_linkedlist/my_list.zig","lineNumber":58,"sourceCode":"        const new_item_ptr = &self.items[self.items.len - 1];\n        new_item_ptr.* = item;\n    }\n\n    // 获取列表长度（当前元素数量）\n    pub fn getSize(self: *Self) usize {\n        return self.items.len;\n    }\n\n    // 获取列表容量\n    pub fn getCapacity(self: *Self) usize {\n        return self.capacity;\n    }\n\n    // 访问元素\n    pub fn get(self: *Self, index: usize) i32 {\n        // 索引如果越界，则抛出异常，下同\n        if (index < 0 or index >= self.items.len) {\n            @panic(\"索引越界\");\n        }\n        return self.items[index];\n    }\n\n    // 更新元素\n    pub fn set(self: *Self, index: usize, num: i32) void {\n        // 索引如果越界，则抛出异常，下同\n        if (index < 0 or index >= self.items.len) {\n            @panic(\"索引越界\");\n        }\n        self.items[index] = num;\n    }\n\n    // 在中间插入元素\n    pub fn insert(self: *Self, index: usize, item: i32) !void {\n        if (index < 0 or index >= self.items.len) {\n            @panic(\"索引越界\");\n        }","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/codes/zig/chapter_array_and_linkedlist/my_list.zig#L40-L76","documentation":"@panic in get(index) of the Zig MyList when the index is outside the live element range. Because index is typed usize (unsigned), the `index < 0` clause is effectively dead code — the real guard is `index >= self.items.len`. A Zig @panic is not catchable; it aborts the process, so callers must validate before calling. get() reads a single element and never changes the list length.","triggerScenarios":"Calling get() on an empty list (len == 0, every index >= 0 panics); index >= items.len after removals shrank the slice; passing a usize computed from an unchecked source.","commonSituations":"Indexing with a value derived from `items.len` captured before removals; deserialized/parsed index not clamped; assuming the `index < 0` branch protects negatives (impossible for usize).","solutions":["Check `index < list.getSize()` (and that the list is non-empty) before get().","Because usize cannot be negative, only validate the upper bound.","Re-fetch getSize() immediately before indexing if the list may have mutated.","For bulk reads, iterate with `for (list.items)` which is bounds-safe by construction."],"exampleFix":"// before: @panics (aborts) when index >= len\nconst v = list.get(idx);\n\n// after\nif (idx < list.getSize()) {\n    const v = list.get(idx);\n}","handlingStrategy":"validation","validationCode":"// usize cannot be negative; validate upper bound only.\nif (idx < list.getSize()) {\n    const v = list.get(idx);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Zig @panic cannot be caught — always validate before get().","Since index is usize, only the upper bound (index < len) can fail; check it.","Prefer `for (list.items)` for iteration — it is bounds-safe by construction."],"tags":["zig","index-out-of-bounds","list","get","usize","validation"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}