{"record":{"id":"cc725149fcd14dc8","repo":"stride3d/stride","slug":"indices-for-matrix-run-from-0-to-15-inclusive","errorCode":null,"errorMessage":"Indices for Matrix run from 0 to 15, inclusive.","messagePattern":"Indices for Matrix run from 0 to 15, inclusive\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core.Mathematics/Matrix.cs","lineNumber":424,"sourceCode":"            return index switch\n            {\n                0 => M11,\n                1 => M12,\n                2 => M13,\n                3 => M14,\n                4 => M21,\n                5 => M22,\n                6 => M23,\n                7 => M24,\n                8 => M31,\n                9 => M32,\n                10 => M33,\n                11 => M34,\n                12 => M41,\n                13 => M42,\n                14 => M43,\n                15 => M44,\n                _ => throw new ArgumentOutOfRangeException(nameof(index), \"Indices for Matrix run from 0 to 15, inclusive.\"),\n            };\n        }\n\n        set\n        {\n            switch (index)\n            {\n                case 0: M11 = value; break;\n                case 1: M12 = value; break;\n                case 2: M13 = value; break;\n                case 3: M14 = value; break;\n                case 4: M21 = value; break;\n                case 5: M22 = value; break;\n                case 6: M23 = value; break;\n                case 7: M24 = value; break;\n                case 8: M31 = value; break;\n                case 9: M32 = value; break;\n                case 10: M33 = value; break;","sourceCodeStart":406,"sourceCodeEnd":442,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core.Mathematics/Matrix.cs#L406-L442","documentation":"Indexer bounds guard on Matrix this[index]: the requested component index is outside [0,15]; the switch over the sixteen M11..M44 entries fell to the default case. The faulting input is the index argument to the indexer.","triggerScenarios":"Reading m[index] with index outside 0..15, e.g. iterating with a wrong bound, negative index, or a linear index computed from row/col arithmetic that overflowed (e.g. row*4+col with row=4).","commonSituations":"Flattened matrix serialization loops with off-by-one bounds; index math bugs like (row-1)*4+col misapplied; copy-pasted code from 3x3 matrices into 4x4 access.","solutions":["Validate: if (index >= 0 && index < 16) before access","Recheck row/col to linear index math (valid: row*4+col for 0<=row,col<4)","Bound loops to i < 16"],"exampleFix":"// before\nvar m11 = m[(row - 1) * 3 + (col - 1)]; // 3x3 math on 4x4 gives wrong/invalid index\n// after\nvar m11 = m[row * 4 + col]; // valid when 0 <= row, col < 4","handlingStrategy":"validation","validationCode":"if (index is < 0 or > 15)\n    throw new ArgumentOutOfRangeException(nameof(index));\nvar value = m[index];","typeGuard":"static bool IsValidMatrixIndex(int i) => (uint)i < 16;\nstatic int ToMatrixIndex(int row, int col) =>\n    (row, col) is { Item1: >= 0 and < 4, Item2: >= 0 and < 4 } ? row * 4 + col : throw new ArgumentOutOfRangeException();","tryCatchPattern":"try { value = m[index]; }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"index\")\n{\n    // log index and context; use identity/default\n}","preventionTips":["Double-check row*4+col math and 0-based vs 1-based conventions","Bound loops to 16","Write a row/col accessor helper that validates both before computing the linear index"],"tags":["csharp","stride","mathematics","matrix","indexer","argument-out-of-range"],"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-15T23:17:13.987Z"}