{"record":{"id":"f4730f373883cca9","repo":"stride3d/stride","slug":"the-destination-array-must-be-of-same-length-or-larger-f4730f","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/engine/Stride.BepuPhysics/Stride.BepuPhysics/Systems/ShapeCacheSystem.cs","lineNumber":300,"sourceCode":"            Orthogonalize(basisIn, basisOut);\n            o.X = basisOut[0].Length();\n            o.Y = basisOut[1].Length();\n            o.Z = basisOut[2].Length();\n        }\n        else\n        {\n            o.X = matrix.Row1.Length();\n            o.Y = matrix.Row2.Length();\n            o.Z = matrix.Row3.Length();\n        }\n\n        return o;\n\n        static void Orthogonalize(ReadOnlySpan<Vector3> source, Span<Vector3> destination)\n        {\n            // Dump of strides' method to strip the memory alloc, refer to Vector3.Orthogonalize\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                Vector3 newVector = source[i];\n\n                for (int r = 0; r < i; ++r)\n                {\n                    newVector -= (Vector3.Dot(destination[r], newVector) / Vector3.Dot(destination[r], destination[r])) * destination[r];\n                }\n\n                destination[i] = newVector;\n            }\n        }\n    }\n\n    /// <summary>\n    /// Hold onto this to keep the cache and the bepu shape for the corresponding mesh alive\n    /// </summary>","sourceCodeStart":282,"sourceCodeEnd":318,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.BepuPhysics/Stride.BepuPhysics/Systems/ShapeCacheSystem.cs#L282-L318","documentation":"ShapeCacheSystem.GetClosestToDecomposableScale uses a local Orthogonalize helper (a zero-allocation dump of Stride's Vector3.Orthogonalize) which requires the destination span to be at least as long as the source span. When the caller supplies a smaller destination buffer, the method throws ArgumentOutOfRangeException naming 'destination'. This mirrors the standard contract of the original vector-math API it replaces.","triggerScenarios":"Calling GetClosestToDecomposableScale with shape vertex/point data whose buffer count exceeds the scratch span passed as 'destination' — e.g. a fixed-size stackalloc buffer used with a larger source array.","commonSituations":"Scale-conversion code for convex shapes where the number of points varies (convex hulls with more vertices than the preallocated span); buffer sized for one shape type reused for a bigger shape after a mesh/hull change.","solutions":["Allocate or stackalloc the destination span with length >= source.Length before calling","Size scratch buffers from the actual point count of the shape rather than a fixed constant","Add an explicit destination.Length >= source.Length check at the call site with a fallback path","If using fixed buffers, cap/reject source data that exceeds the buffer capacity instead of letting it throw"],"exampleFix":"// before\nSpan<Vector3> dest = stackalloc Vector3[8];\nOrthogonalize(source, dest); // throws if source.Length > 8\n// after\nSpan<Vector3> dest = source.Length <= 8 ? stackalloc Vector3[8] : new Vector3[source.Length];\nOrthogonalize(source, dest);","handlingStrategy":"validation","validationCode":"if (destination.Length < source.Length)\n    destination = new Vector3[source.Length]; // or resize before calling","typeGuard":null,"tryCatchPattern":"try { result = GetClosestToDecomposableScale(...); } catch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"destination\") { /* grow buffer and retry */ }","preventionTips":["Size destination spans from source.Length, not fixed constants","Stackalloc only with a known upper bound and fall back to heap for larger inputs","Reuse a scratch pool sized to the largest expected shape point count"],"tags":["physics","argument-out-of-range","buffers"],"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"}