{"record":{"id":"257c602e4ab0cc84","repo":"LuckyPennySoftware/AutoMapper","slug":"not-enough-room-in-destination-array-destination","errorCode":null,"errorMessage":"Not enough room in destination array {destination}","messagePattern":"Not enough room in destination array (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/AutoMapper/Mappers/CollectionMapper.cs","lineNumber":260,"sourceCode":"            bool MustMap(Type sourceType, Type destinationType) => !destinationType.IsAssignableFrom(sourceType) ||\n                configuration.FindTypeMapFor(sourceType, destinationType) != null;\n        }\n    }\n}\npublic readonly struct MultidimensionalArrayFiller(Array destination)\n{\n    readonly int[] _indices = new int[destination.Rank];\n    public void NewValue(object value)\n    {\n        var dimension = destination.Rank - 1;\n        var changedDimension = false;\n        while (_indices[dimension] == destination.GetLength(dimension))\n        {\n            _indices[dimension] = 0;\n            dimension--;\n            if (dimension < 0)\n            {\n                throw new InvalidOperationException(\"Not enough room in destination array \" + destination);\n            }\n            _indices[dimension]++;\n            changedDimension = true;\n        }\n        destination.SetValue(value, _indices);\n        if (changedDimension)\n        {\n            _indices[dimension + 1]++;\n        }\n        else\n        {\n            _indices[dimension]++;\n        }\n    }\n}","sourceCodeStart":242,"sourceCodeEnd":275,"githubUrl":"https://github.com/LuckyPennySoftware/AutoMapper/blob/dfa6dd587c5854b4beee5934beb39ba6e9569b84/src/AutoMapper/Mappers/CollectionMapper.cs#L242-L275","documentation":"Maps a flat source collection into a multidimensional (ranked) destination array via MultidimensionalArrayFiller, filling cells dimension by dimension. The throw fires when the dimension counter walks past every dimension (dimension < 0) while NewValue still has elements to place, i.e. the source holds more values than the destination array's total cell capacity. It is the library's way of refusing a silent truncation when shapes do not match.","triggerScenarios":"Calling Map from an IEnumerable/array onto a System.Array with Rank > 1 (e.g. int[,], string[,,]) where source.Count() exceeds the product of destination.GetLength(d) over all d. Each source element invokes MultidimensionalArrayFiller.NewValue, so one extra element after the last cell is full triggers the throw.","commonSituations":"Destination array allocated with stale/wrong dimension lengths after the source grew; off-by-one when sizing dims at runtime; data import where row/column counts exceed the pre-allocated matrix; reusing a fixed-size buffer for variable-size input.","solutions":["Size the destination multidimensional array so the product of all GetLength(d) is >= source element count before mapping.","Trim or page the source collection so its count fits the fixed destination shape.","Switch the destination to a 1D array or List<T> when the multidimensional shape is not actually required.","Pre-validate dimensions against the source and surface a domain error instead of letting AutoMapper throw."],"exampleFix":"// before\nvar dest = new int[2, 2];\nmapper.Map(srcList, dest); // throws if srcList.Count > 4\n\n// after\nvar rows = (int)Math.Ceiling(Math.Sqrt(srcList.Count));\nvar dest = new int[rows, srcList.Count % rows == 0 ? srcList.Count / rows : srcList.Count / rows + 1];\nmapper.Map(srcList, dest);","handlingStrategy":"validation","validationCode":"static long ArrayCapacity(Array a)\n{\n    long capacity = 1;\n    for (int d = 0; d < a.Rank; d++) capacity *= a.GetLength(d);\n    return capacity;\n}\n\nlong needed = source.Cast<object>().LongCount();\nif (needed > ArrayCapacity(destination))\n    throw new ArgumentException($\"Destination array capacity ({ArrayCapacity(destination)}) < source count ({needed}).\");\n\nmapper.Map(source, destination);","typeGuard":null,"tryCatchPattern":"try { mapper.Map(source, destination); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Not enough room in destination array\"))\n{\n    // recompute destination dimensions or trim the source, then retry once\n}","preventionTips":["Always derive multidimensional destination lengths from the source count, not from hardcoded constants.","Prefer List<T> or a 1D array when the multidimensional shape is not a hard requirement.","Add a unit test that maps the largest expected source and asserts no throw."],"tags":["arrays","multidimensional-array","collection-mapping","capacity"],"backgroundTag":null,"analyzedSha":"dfa6dd587c5854b4beee5934beb39ba6e9569b84","analyzedAt":"2026-08-13T19:56:33.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}