{"record":{"id":"e639466976837389","repo":"diesel-rs/diesel","slug":"out-of-range-integral-type-conversion-attempted","errorCode":null,"errorMessage":"out of range integral type conversion attempted","messagePattern":"out of range integral type conversion attempted","errorType":"exception","errorClass":"std::io::Error","httpStatus":null,"severity":"error","filePath":"diesel/src/sqlite/connection/sqlite_blob.rs","lineNumber":85,"sourceCode":"        //     If an error occurs while committing the transaction, an error code is returned and\n        //     the transaction rolled back.\n        //\n        // As we are in read-only mode here, this is not an issue\n        let close_result = unsafe { ffi::sqlite3_blob_close(self.blob.as_ptr()) };\n\n        if close_result != ffi::SQLITE_OK {\n            let error_message = super::error_message(close_result);\n            return Err(crate::result::Error::ClosingHandle(error_message));\n        }\n\n        Ok(())\n    }\n}\n\n#[cfg(feature = \"std\")]\n#[allow(clippy::std_instead_of_core)] // needs a newer rust version\nfn to_io_error(error: core::num::TryFromIntError) -> std::io::Error {\n    std::io::Error::new(std::io::ErrorKind::InvalidInput, Box::new(error))\n}\n\n// SEE https://github.com/rust-lang/rust/issues/48331\n#[cfg(feature = \"std\")]\nimpl std::io::Read for SqliteReadOnlyBlob<'_> {\n    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {\n        let buflen: i32 = buf.len().try_into().map_err(to_io_error)?;\n        let offset: i32 = self.read_index.try_into().map_err(to_io_error)?;\n\n        // From the sqlite docs:\n        //\n        // > If offset iOffset is less than N bytes from the end of the BLOB, SQLITE_ERROR is returned and no data is read.\n        //\n        // Thus we need to make sure to not provide a buffer that is too big for the remaining data\n        // from the blob.\n        let read_length: i32 = (i32::try_from(self.blob_size)\n            .map_err(to_io_error)?\n            .saturating_sub(offset))","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/diesel-rs/diesel/blob/6fa6ed01b24b24248ab2a611698d0a7c6a2e9120/diesel/src/sqlite/connection/sqlite_blob.rs#L67-L103","documentation":"A `TryFromIntError` from a failed integer conversion (e.g. `usize -> u32` via `try_into`) was wrapped into a `std::io::Error` with kind `InvalidInput`. Diesel's SQLite blob I/O adapters convert file offsets/sizes to SQLite's expected integer types, and this fires when a value does not fit the target integral type.","triggerScenarios":"Using `SqliteReadOnlyBlob`/blob IO helpers where an offset, length, or size value exceeds the range of the target integer type during the conversion performed in `to_io_error`.","commonSituations":"Reading or writing a blob region with an offset/length larger than 2^31-1 (exceeding `i32`/`u32` bounds), often from very large blobs or bad computed offsets on 64-bit platforms.","solutions":["Check the offset/length values passed to the blob read/write APIs; keep them within `i32`/`u32` range","Compute sizes/offsets with checked arithmetic (`checked_add`/`try_into`) before passing them in","If the blob genuinely exceeds the limit, split it or use a different storage strategy","Inspect the underlying `TryFromIntError` message to identify which conversion overflowed"],"exampleFix":"// before\nlet offset: u32 = huge_usize.try_into().unwrap();\n// after\nlet offset: u32 = u32::try_from(huge_usize).map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidInput, e))?;","handlingStrategy":"validation","validationCode":"fn valid_blob_range(offset: usize, len: usize) -> bool {\n    u32::try_from(offset).is_ok() && u32::try_from(len).is_ok() && offset.checked_add(len).is_some()\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Range-check offsets and lengths against u32::MAX before blob IO","Use checked arithmetic for computed blob offsets"],"tags":["rust","sqlite","integer-overflow","blob-io"],"backgroundTag":"value-out-of-range","analyzedSha":"6fa6ed01b24b24248ab2a611698d0a7c6a2e9120","analyzedAt":"2026-09-07T01:50:13.074Z","contentChangedAt":"2026-09-07T01:50:13.074Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}