{"record":{"id":"b338102b7f8f133a","repo":"bevyengine/bevy","slug":"array-layout-should-be-valid","errorCode":null,"errorMessage":"array layout should be valid","messagePattern":"array layout should be valid","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"crates/bevy_ecs/src/storage/blob_array.rs","lineNumber":264,"sourceCode":"            let item = self.get_unchecked_mut(last_element_index).promote();\n            // SAFETY:\n            unsafe { drop(item) };\n            self.drop = Some(drop);\n        }\n    }\n\n    /// Allocate a block of memory for the array. This should be used to initialize the array, do not use this\n    /// method if there are already elements stored in the array - use [`Self::realloc`] instead.\n    ///\n    /// # Panics\n    /// - Panics if the new capacity overflows `isize::MAX` bytes.\n    /// - Panics if the allocation causes an out-of-memory error.\n    pub(super) fn alloc(&mut self, capacity: NonZeroUsize) {\n        #[cfg(debug_assertions)]\n        debug_assert_eq!(self.capacity, 0);\n        if !self.is_zst() {\n            let new_layout = self.item_layout.repeat_packed(capacity.get());\n            let new_layout = new_layout.expect(\"array layout should be valid\");\n            // SAFETY: layout has non-zero size because capacity > 0, and the blob isn't ZST (`self.is_zst` == false)\n            let new_data = unsafe { alloc::alloc::alloc(new_layout) };\n            self.data = NonNull::new(new_data).unwrap_or_else(|| handle_alloc_error(new_layout));\n        }\n        #[cfg(debug_assertions)]\n        {\n            self.capacity = capacity.into();\n        }\n    }\n\n    /// Reallocate memory for this array.\n    /// For example, if the length (number of stored elements) reached the capacity (number of elements the current allocation can store),\n    /// you might want to use this method to increase the allocation, so more data can be stored in the array.\n    ///\n    /// # Panics\n    /// - Panics if the new capacity overflows `isize::MAX` bytes.\n    /// - Panics if the allocation causes an out-of-memory error.\n    ///","sourceCodeStart":246,"sourceCodeEnd":282,"githubUrl":"https://github.com/bevyengine/bevy/blob/396ca727080776bd313bb892423b7d94e03b81b4/crates/bevy_ecs/src/storage/blob_array.rs#L246-L282","documentation":"BlobArray::alloc computes the allocation layout as item_layout.repeat_packed(capacity) and expects it to be valid. Layout::repeat_packed only fails when capacity * item_size overflows the address space (result must stay within usize/isize bounds), so this panic means the requested element count times the item size is unrepresentable - a corrupted or astronomically large capacity, not a normal out-of-memory.","triggerScenarios":"A capacity value computed from untrusted or serialized input (entity counts, table sizing) that overflows before reaching alloc; arithmetic bugs producing near-usize::MAX element counts; huge component sizes multiplied by large capacities.","commonSituations":"Deserializing untrusted length prefixes; capacity math using wrapping/underflowed intermediates; 32-bit targets where the address space is exhausted by modest counts of large components.","solutions":["Trace where the capacity value comes from and fix the size computation - the panic means bytes = capacity x item_size overflowed.","Validate/sanitize any size read from files/network (bounds caps, checked_mul) before it drives storage sizing.","If the allocation is legitimately huge, batch it into smaller chunks or shrink the component layout."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"fn capacity_is_representable(item_layout: Layout, capacity: usize) -> bool {\n    item_layout.repeat_packed(capacity).is_ok()\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use checked arithmetic (checked_mul/checked_add) when computing capacities from external data.","Cap deserialized length/count fields at sane maxima before driving storage.","Read this panic as a size-computation bug, not an OOM: inspect the capacity in a debugger."],"tags":["bevy","ecs","storage","allocation","overflow","panic"],"backgroundTag":"allocation-size-overflow","analyzedSha":"396ca727080776bd313bb892423b7d94e03b81b4","analyzedAt":"2026-08-20T16:12:39.808Z","contentChangedAt":"2026-08-20T16:12:39.808Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}