From f31f4dc40dc51d338b378f2513d298497c2d889b Mon Sep 17 00:00:00 2001 From: HelloWorldTeraByte Date: Tue, 3 Mar 2026 11:28:09 +1300 Subject: [PATCH] Fixed offset calculation overflowing on large SD cards --- CHANGELOG.md | 4 +++- src/fs.rs | 9 +++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 65a072e1..6d95b225 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## Unreleased -- +### Fixed + +- Fixed offset calculation overflow in `lfs_config_read` and `lfs_config_prog` if the offset is larger than `u32::MAX`. ## [v0.6.1](https://github.com/trussed-dev/littlefs2/releases/tag/0.6.1) - 2025-03-04 diff --git a/src/fs.rs b/src/fs.rs index 5ecbd60f..82ec300e 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -536,8 +536,9 @@ impl Filesystem<'_, Storage> { // println!("in lfs_config_read for {} bytes", size); let storage = unsafe { &mut *((*c).context as *mut Storage) }; debug_assert!(!c.is_null()); - let block_size = unsafe { c.read().block_size }; - let off = (block * block_size + off) as usize; + // let block_size = unsafe { c.read().block_size }; + let block_size = Storage::BLOCK_SIZE; + let off = block as usize * block_size + off as usize; let buf: &mut [u8] = unsafe { slice::from_raw_parts_mut(buffer as *mut u8, size as usize) }; error_code_from(storage.read(off, buf)) @@ -556,8 +557,8 @@ impl Filesystem<'_, Storage> { let storage = unsafe { &mut *((*c).context as *mut Storage) }; debug_assert!(!c.is_null()); // let block_size = unsafe { c.read().block_size }; - let block_size = Storage::BLOCK_SIZE as u32; - let off = (block * block_size + off) as usize; + let block_size = Storage::BLOCK_SIZE; + let off = block as usize * block_size + off as usize; let buf: &[u8] = unsafe { slice::from_raw_parts(buffer as *const u8, size as usize) }; error_code_from(storage.write(off, buf))