Skip to content

Commit f31f4dc

Browse files
HelloWorldTeraByterobin-nitrokey
authored andcommitted
Fixed offset calculation overflowing on large SD cards
1 parent 76c038e commit f31f4dc

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## Unreleased
99

10-
-
10+
### Fixed
11+
12+
- Fixed offset calculation overflow in `lfs_config_read` and `lfs_config_prog` if the offset is larger than `u32::MAX`.
1113

1214
## [v0.6.1](https://github.com/trussed-dev/littlefs2/releases/tag/0.6.1) - 2025-03-04
1315

src/fs.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -536,8 +536,9 @@ impl<Storage: driver::Storage> Filesystem<'_, Storage> {
536536
// println!("in lfs_config_read for {} bytes", size);
537537
let storage = unsafe { &mut *((*c).context as *mut Storage) };
538538
debug_assert!(!c.is_null());
539-
let block_size = unsafe { c.read().block_size };
540-
let off = (block * block_size + off) as usize;
539+
// let block_size = unsafe { c.read().block_size };
540+
let block_size = Storage::BLOCK_SIZE;
541+
let off = block as usize * block_size + off as usize;
541542
let buf: &mut [u8] = unsafe { slice::from_raw_parts_mut(buffer as *mut u8, size as usize) };
542543

543544
error_code_from(storage.read(off, buf))
@@ -556,8 +557,8 @@ impl<Storage: driver::Storage> Filesystem<'_, Storage> {
556557
let storage = unsafe { &mut *((*c).context as *mut Storage) };
557558
debug_assert!(!c.is_null());
558559
// let block_size = unsafe { c.read().block_size };
559-
let block_size = Storage::BLOCK_SIZE as u32;
560-
let off = (block * block_size + off) as usize;
560+
let block_size = Storage::BLOCK_SIZE;
561+
let off = block as usize * block_size + off as usize;
561562
let buf: &[u8] = unsafe { slice::from_raw_parts(buffer as *const u8, size as usize) };
562563

563564
error_code_from(storage.write(off, buf))

0 commit comments

Comments
 (0)