{"record":{"id":"1cb2e7e1e921d7bc","repo":"pola-rs/polars","slug":"the-length-of-the-values-must-be-equal-to-the-last","errorCode":null,"errorMessage":"The length of the values must be equal to the last offset value","messagePattern":"The length of the values must be equal to the last offset value","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/polars-arrow/src/array/utf8/mutable_values.rs","lineNumber":110,"sourceCode":"    ///\n    /// # Panic\n    /// This function does not panic iff:\n    /// * `offsets.last()` is greater than `values.len()`\n    /// * The `dtype`'s [`crate::datatypes::PhysicalType`] is equal to either `Utf8` or `LargeUtf8`.\n    ///\n    /// # Safety\n    /// This function is safe iff:\n    /// * the offsets are monotonically increasing\n    /// * The `values` between two consecutive `offsets` are not valid utf8\n    /// # Implementation\n    /// This function is `O(1)`\n    pub unsafe fn new_unchecked(\n        dtype: ArrowDataType,\n        offsets: Offsets<O>,\n        values: Vec<u8>,\n    ) -> Self {\n        try_check_offsets_bounds(&offsets, values.len())\n            .expect(\"The length of the values must be equal to the last offset value\");\n\n        if dtype.to_physical_type() != Self::default_dtype().to_physical_type() {\n            panic!(\n                \"MutableUtf8ValuesArray can only be initialized with DataType::Utf8 or DataType::LargeUtf8\"\n            )\n        }\n\n        Self {\n            dtype,\n            offsets,\n            values,\n        }\n    }\n\n    /// Returns the default [`ArrowDataType`] of this container: [`ArrowDataType::Utf8`] or [`ArrowDataType::LargeUtf8`]\n    /// depending on the generic [`Offset`].\n    pub fn default_dtype() -> ArrowDataType {\n        Utf8Array::<O>::default_dtype()","sourceCodeStart":92,"sourceCodeEnd":128,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/crates/polars-arrow/src/array/utf8/mutable_values.rs#L92-L128","documentation":"`MutableUtf8ValuesArray::new_unchecked` is an `unsafe` constructor that trusts the caller-supplied offsets and values, but it still validates that the last offset equals `values.len()` (via `try_check_offsets_bounds`) so the offsets actually index the value buffer. If the final offset doesn't match the values length, the resulting array would read out of bounds, so it `expect`-panics.","triggerScenarios":"Calling `MutableUtf8ValuesArray::new_unchecked(dtype, offsets, values)` where `*offsets.last()` != `values.len()` — e.g. offsets built for a shorter/longer value buffer, or offsets mutated after construction of `values`.","commonSituations":"Hand-rolled builders of UTF8 arrays in FFI/import code where bytes and offsets are produced by separate passes; truncating or extending the value buffer without recomputing offsets; porting code from `try_new` (which returns Err) to `new_unchecked` without moving the validation.","solutions":["Ensure `offsets.last() == values.len()` before calling, or recompute offsets from the values with `Offsets::from_lengths` / `try_from_iter`.","Use the safe `MutableUtf8ValuesArray::try_new` / `new_from_iter` constructors which validate and return `PolarsResult` instead of panicking.","Fix the producer of `values` so the two buffers are built in lockstep.","Run under debug/tests with the bounds check kept (`try_check_offsets_bounds` first) until correctness is proven."],"exampleFix":"// before\nlet arr = unsafe { MutableUtf8ValuesArray::new_unchecked(dtype, offsets, values) };\n// after\nlet offsets = Offsets::try_from(&values_lens)?; // or rebuild:\nlet arr = MutableUtf8ValuesArray::try_new(dtype, offsets, values)?;","handlingStrategy":"validation","validationCode":"fn offsets_match(offsets: &Offsets<O>, values_len: usize) -> bool {\n    *offsets.last() as usize == values_len\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Build offsets and values together (e.g. `MutableUtf8ValuesArray::try_new` or `new_from_iter`)","Recompute offsets with `Offsets::from_lengths` whenever the value buffer changes","Prefer the safe `try_new` constructor; reserve `new_unchecked` for proven-hot paths with debug assertions","Keep a debug-mode check via `try_check_offsets_bounds` before switching to unchecked"],"tags":["rust","arrow","utf8","offsets","unsafe"],"backgroundTag":"index-out-of-bounds","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}