{"record":{"id":"04b800e9a2aa3a14","repo":"stride3d/stride","slug":"invalid-xinput-device-index-index","errorCode":null,"errorMessage":"Invalid XInput device index {index}","messagePattern":"Invalid XInput device index (.+?)","errorType":"exception","errorClass":"IndexOutOfRangeException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Input/Windows/InputSourceWindowsXInput.cs","lineNumber":107,"sourceCode":"                if (devices[i] == null)\n                {\n                    // Should register controller\n                    if (controllers[i].IsConnected)\n                    {\n                        OpenDevice(i);\n                    }\n                }\n            }\n        }\n\n        /// <summary>\n        /// Opens a new gamepad\n        /// </summary>\n        /// <param name=\"instance\">The gamepad</param>\n        public void OpenDevice(int index)\n        {\n            if (index < 0 || index >= XInputGamePadCount)\n                throw new IndexOutOfRangeException($\"Invalid XInput device index {index}\");\n\n            if (devices[index] != null)\n                throw new InvalidOperationException($\"XInput device already opened {index}\");\n\n            var newGamepad = new GamePadXInput(this, controllers[index], controllerIds[index], index);\n            newGamepad.Disconnected += (sender, args) =>\n            {\n                // Queue device for removal\n                devicesToRemove.Add(index);\n            };\n\n            devices[index] = newGamepad;\n            RegisterDevice(newGamepad);\n        }\n    }\n}\n\n#endif","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Input/Windows/InputSourceWindowsXInput.cs#L89-L125","documentation":"InputSourceWindowsXInput.OpenDevice validates the gamepad index against XInputGamePadCount (4) and throws IndexOutOfRangeException for indices outside 0-3. XInput supports at most four controllers, and devices[] is only sized for valid indices.","triggerScenarios":"Calling OpenDevice with a negative index or an index >= XInputGamePadCount, typically from custom Scan logic or device-restore code that passes a stale/incorrect index.","commonSituations":"Restoring saved device configs that store raw driver indices; custom enumeration code mapping more than four devices to XInput; off-by-one loops over controllers.","solutions":["Clamp or validate the index to 0..XInputGamePadCount-1 before calling OpenDevice.","Map non-XInput devices to a different backend instead of forcing an XInput index.","Fix loop bounds or saved-index logic in custom Scan/restore code."],"exampleFix":"// before\ninputSource.OpenDevice(deviceIndex); // may be > 3\n// after\nif (deviceIndex >= 0 && deviceIndex < 4)\n    inputSource.OpenDevice(deviceIndex);","handlingStrategy":"validation","validationCode":"if (index < 0 || index >= XInputGamePadCount)\n    return; // or clamp: index = Math.Max(0, Math.Min(3, index));\ninputSource.OpenDevice(index);","typeGuard":"bool IsValidXInputIndex(int i) => i >= 0 && i < 4;","tryCatchPattern":"try\n{\n    inputSource.OpenDevice(index);\n}\ncatch (IndexOutOfRangeException)\n{\n    // Log the invalid index; skip this device.\n}","preventionTips":["Clamp indices to 0..3 before calling OpenDevice.","Do not map non-XInput device indices onto the XInput backend.","Fix stored device indices when migrating saved input configurations."],"tags":["xinput","index-out-of-range","gamepad","windows"],"backgroundTag":"index-out-of-range","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}