{"record":{"id":"a542ba957fd1e922","repo":"egametang/ET","slug":"invalid-slot-index-slotindex","errorCode":null,"errorMessage":"invalid slot index: {slotIndex}","messagePattern":"invalid slot index: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"Packages/cn.etetet.item/Scripts/Hotfix/Client/ItemComponentSystem.cs","lineNumber":199,"sourceCode":"        public static void ClearSlot(this ItemComponent self, int slotIndex)\n        {\n            if ((uint)slotIndex >= (uint)self.SlotItems.Count)\n            {\n                return;\n            }\n\n            self.SlotItems[slotIndex] = default;\n        }\n\n        #endregion\n\n        #region 私有方法\n\n        private static void EnsureSlotIndex(ItemComponent self, int slotIndex)\n        {\n            if (slotIndex < 0)\n            {\n                throw new Exception($\"invalid slot index: {slotIndex}\");\n            }\n\n            if (slotIndex >= self.Capacity)\n            {\n                throw new Exception($\"slot index {slotIndex} exceeds capacity {self.Capacity}\");\n            }\n        }\n\n        private static void EnsureSlotContainerSize(ItemComponent self, int size)\n        {\n            if (size <= 0)\n            {\n                return;\n            }\n\n            if (self.SlotItems.Count >= size)\n            {\n                return;","sourceCodeStart":181,"sourceCodeEnd":217,"githubUrl":"https://github.com/egametang/ET/blob/5cab01f7a8bee5f49f4781eebe9e2b1c6d7ebe0f/Packages/cn.etetet.item/Scripts/Hotfix/Client/ItemComponentSystem.cs#L181-L217","documentation":"Thrown by ItemComponentSystem.EnsureSlotIndex when a caller passes a negative slot index to a slot operation (SetSlotItem, UpdateItem, etc.). The inventory component validates that slot indices fall within [0, Capacity) before mutating the SlotItems list; a negative index fails the first guard.","triggerScenarios":"EnsureSlotIndex(self, slotIndex) is invoked at the start of SetSlotItem and UpdateItem. If slotIndex < 0 the method throws immediately. A separate guard (line 204) covers slotIndex >= Capacity.","commonSituations":"Server-sent item data with an unset/default slot index (0 is valid, but -1 is a common sentinel for no slot); client code computing a slot from an off-by-one calculation; deserialized JSON with a missing slot field defaulting to a negative; UI code passing a clicked-slot index before bounds checking.","solutions":["Validate the slot index at the source (network handler, UI event) before calling UpdateItem/SetSlotItem.","If -1 is used as a sentinel for unassigned, convert it to a valid representation (a separate nullable field or a dedicated unslotted collection) before reaching the inventory component.","Add a precondition check or unit test asserting slotIndex >= 0 for all call sites.","Consider clamping or rejecting server payloads with invalid slot indices at the deserialization boundary."],"exampleFix":"// before\nitemComponent.UpdateItem(itemId, slotIndex, configId, count);\n// after\nif ((uint)slotIndex < (uint)itemComponent.Capacity)\n{\n    itemComponent.UpdateItem(itemId, slotIndex, configId, count);\n}\nelse\n{\n    Log.Warning($\"rejected item update with invalid slot {slotIndex}\");\n}","handlingStrategy":"validation","validationCode":"// Validate before calling slot operations\npublic static bool TryUpdateItem(this ItemComponent self, long itemId, int slotIndex, int configId, int count)\n{\n    if ((uint)slotIndex >= (uint)self.Capacity) return false;\n    self.UpdateItem(itemId, slotIndex, configId, count);\n    return true;\n}","typeGuard":"// Guard: true if the slot index is within valid inventory bounds\nstatic bool IsValidSlot(ItemComponent component, int slotIndex)\n    => slotIndex >= 0 && slotIndex < component.Capacity;","tryCatchPattern":null,"preventionTips":["Validate slot indices at the network deserialization boundary before they reach the inventory component.","Use a sentinel-free representation for unslotted items (separate field or collection) instead of negative indices.","Add unit tests covering slotIndex = -1, 0, Capacity-1, Capacity to lock in boundary behavior."],"tags":["gameplay","inventory","validation","argument"],"backgroundTag":null,"analyzedSha":"5cab01f7a8bee5f49f4781eebe9e2b1c6d7ebe0f","analyzedAt":"2026-08-13T21:10:40.377Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}