{"record":{"id":"0ed3b78003b057e0","repo":"peass-ng/PEASS-ng","slug":"index-is-not-a-valid-index-in-the-actioncollection","errorCode":null,"errorMessage":"Index is not a valid index in the ActionCollection","messagePattern":"Index is not a valid index in the ActionCollection","errorType":"exception","errorClass":"System.ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"winPEAS/winPEASexe/winPEAS/TaskScheduler/ActionCollection.cs","lineNumber":249,"sourceCode":"            {\n                if (v2Coll != null)\n                    return Action.CreateAction(v2Coll[++index]);\n                if (v1Task != null)\n                {\n                    if (SupportV1Conversion)\n                        return v1Actions[index];\n                    else\n                    {\n                        if (index == 0)\n                            return v1Actions[0];\n                    }\n                }\n                throw new ArgumentOutOfRangeException();\n            }\n            set\n            {\n                if (index < 0 || Count <= index)\n                    throw new ArgumentOutOfRangeException(nameof(index), index, \"Index is not a valid index in the ActionCollection\");\n                var orig = this[index].Clone();\n                if (v2Coll != null)\n                {\n                    inV2set = true;\n                    try\n                    {\n                        Insert(index, value);\n                        RemoveAt(index + 1);\n                    }\n                    finally\n                    {\n                        inV2set = false;\n                    }\n                }\n                else\n                {\n                    v1Actions[index] = value;\n                    SaveV1Actions();","sourceCodeStart":231,"sourceCodeEnd":267,"githubUrl":"https://github.com/peass-ng/PEASS-ng/blob/53fb989abc2219826385683a6fee826bd6cd38d6/winPEAS/winPEASexe/winPEAS/TaskScheduler/ActionCollection.cs#L231-L267","documentation":"The ActionCollection indexer setter validates that the index refers to an existing action; if index is negative or >= Count it throws ArgumentOutOfRangeException with this message. This protects the underlying COM collection (v2) and the internal v1 action list from out-of-range writes.","triggerScenarios":"Assigning actions[Count] to 'append' (the setter does not append — use Add); looping with an upper bound from a stale count captured before removals; passing a saved index after the collection was modified.","commonSituations":"Code that assumes list-style append-by-index semantics; UI list updates after actions were deleted elsewhere; deserialization logic writing items at their original indices into a shorter collection.","solutions":["Use Actions.Add(newAction) instead of the indexer to append new actions","Re-check Count immediately before each indexed write; iterate with for (int i = 0; i < actions.Count; i++)","If replacing, capture a fresh index from IndexOf rather than a cached value","Catch ArgumentOutOfRangeException and treat as a logic bug — log collection state for diagnosis"],"exampleFix":"// before\nint saved = actions.Count;\n// ... other code removes actions ...\nactions[saved] = new ExecAction(\"cmd.exe\"); // out of range\n// after\nactions.Add(new ExecAction(\"cmd.exe\")); // append via Add\n// or, for replace:\nint idx = actions.IndexOf(existing);\nif (idx >= 0 && idx < actions.Count) actions[idx] = new ExecAction(\"cmd.exe\");","handlingStrategy":"validation","validationCode":"static bool IsValidActionIndex(ActionCollection actions, int i) => i >= 0 && i < actions.Count;","typeGuard":null,"tryCatchPattern":"try { actions[index] = newAction; }\ncatch (ArgumentOutOfRangeException ex)\n{\n    log.Error($\"Action index {index} invalid (Count={actions.Count}); use Add to append.\");\n}","preventionTips":["Use Add for appending — the indexer only replaces existing items","Re-read Count right before indexed writes; never cache indices across modifications","Iterate with for loops bounded by the live Count","Fetch indices via IndexOf instead of remembering old positions"],"tags":["csharp","taskscheduler","index-out-of-range","collection"],"backgroundTag":"collection-index-out-of-range","analyzedSha":"53fb989abc2219826385683a6fee826bd6cd38d6","analyzedAt":"2026-09-02T04:25:09.259Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T11:17:12.671Z"}