{"record":{"id":"638103bbc133c234","repo":"stride3d/stride","slug":"the-destination-array-must-be-of-same-length-or-larger-638103","errorCode":null,"errorMessage":"The destination array must be of same length or larger length than the source array.","messagePattern":"The destination array must be of same length or larger length than the source array\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core.Mathematics/Double3.cs","lineNumber":975,"sourceCode":"    /// tend to be numerically unstable. The numeric stability decreases according to the vectors\n    /// position in the list so that the first vector is the most stable and the last vector is the\n    /// least stable.</para>\n    /// </remarks>\n    /// <exception cref=\"ArgumentNullException\">Thrown when <paramref name=\"source\"/> or <paramref name=\"destination\"/> is <c>null</c>.</exception>\n    /// <exception cref=\"ArgumentOutOfRangeException\">Thrown when <paramref name=\"destination\"/> is shorter in length than <paramref name=\"source\"/>.</exception>\n    public static void Orthogonalize(Double3[] destination, params Double3[] source)\n    {\n        //Uses the modified Gram-Schmidt process.\n        //q1 = m1\n        //q2 = m2 - ((q1 ⋅ m2) / (q1 ⋅ q1)) * q1\n        //q3 = m3 - ((q1 ⋅ m3) / (q1 ⋅ q1)) * q1 - ((q2 ⋅ m3) / (q2 ⋅ q2)) * q2\n        //q4 = m4 - ((q1 ⋅ m4) / (q1 ⋅ q1)) * q1 - ((q2 ⋅ m4) / (q2 ⋅ q2)) * q2 - ((q3 ⋅ m4) / (q3 ⋅ q3)) * q3\n        //q5 = ...\n\n        ArgumentNullException.ThrowIfNull(source);\n        ArgumentNullException.ThrowIfNull(destination);\n        if (destination.Length < source.Length)\n            throw new ArgumentOutOfRangeException(nameof(destination), \"The destination array must be of same length or larger length than the source array.\");\n\n        for (int i = 0; i < source.Length; ++i)\n        {\n            Double3 newvector = source[i];\n\n            for (int r = 0; r < i; ++r)\n            {\n                newvector -= Dot(destination[r], newvector) / Dot(destination[r], destination[r]) * destination[r];\n            }\n\n            destination[i] = newvector;\n        }\n    }\n\n    /// <summary>\n    /// Orthonormalizes a list of vectors.\n    /// </summary>\n    /// <param name=\"destination\">The list of orthonormalized vectors.</param>","sourceCodeStart":957,"sourceCodeEnd":993,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core.Mathematics/Double3.cs#L957-L993","documentation":"Thrown by Double3's array-based Gram-Schmidt orthogonalization (the method containing this check) when the destination array is shorter than the source. Each orthogonalized vector is written at the same index into destination, so a smaller destination would overflow. Stride validates lengths before the algorithm and throws ArgumentOutOfRangeException naming 'destination'.","triggerScenarios":"Calling the Double3 Orthogonalize-style API (Double3[] source, ref readonly Matrix-ish param, Double3[] destination) with destination.Length < source.Length.","commonSituations":"Reusing a smaller cached buffer; allocating destination from a different (smaller) array; swapping source/destination arguments by mistake.","solutions":["Allocate destination with at least source.Length elements: new Double3[source.Length]","Reuse a scratch buffer only after checking its Length against source.Length","Confirm source/destination are not reversed"],"exampleFix":"// before\nvar dest = new Double3[src.Length / 2];\nDouble3.Orthogonalize(src, in m, dest);\n// after\nvar dest = new Double3[src.Length];\nDouble3.Orthogonalize(src, in m, dest);","handlingStrategy":"validation","validationCode":"if (dest is null || dest.Length < src.Length) dest = new Double3[src.Length];","typeGuard":"static bool CanOrthogonalize(Double3[] src, Double3[] dest) => src != null && dest != null && dest.Length >= src.Length;","tryCatchPattern":"try { Double3.Orthogonalize(src, in m, dest); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"destination\") { dest = new Double3[src.Length]; Double3.Orthogonalize(src, in m, dest); }","preventionTips":["Match destination length to source length for every array-based math API","Use a shared EnsureCapacity helper for pooled buffers","Keep source/destination in named parameters to avoid swapping them"],"tags":["argument-out-of-range","arrays","mathematics"],"backgroundTag":"argument-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"}