Skip to content

Commit 50b8dec

Browse files
committed
Fix format_volume panic in debug build for small FAT12 volumes
1 parent 2d0d3b6 commit 50b8dec

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ New features:
2727
* Fix time encoding during a leap second if using `chrono`.
2828
* Create directory entry with `VOLUME_ID` attribute when formatting if volume label was set in `FormatVolumeOptions`.
2929
* Fix creating directory entries when `lfn` feature is enabled and `alloc` feature is disabled
30+
* Fix `format_volume` function panicking in debug build for FAT12 volumes with size below 1 MB
3031
* Remove `byteorder` dependency.
3132
* Bump up minimal Rust compiler version to 1.46.0.
3233
* Build the crate using the 2018 edition.

src/boot_sector.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -513,11 +513,12 @@ fn determine_bytes_per_cluster(total_bytes: u64, bytes_per_sector: u16, fat_type
513513
}
514514
}
515515
};
516-
debug_assert!(bytes_per_cluster.is_power_of_two());
517-
cmp::min(
516+
let bytes_per_cluster_clamped = cmp::min(
518517
cmp::max(bytes_per_cluster, u32::from(bytes_per_sector)),
519518
MAX_CLUSTER_SIZE,
520-
)
519+
);
520+
debug_assert!(bytes_per_cluster_clamped.is_power_of_two());
521+
bytes_per_cluster_clamped
521522
}
522523

523524
fn determine_sectors_per_fat(
@@ -817,6 +818,7 @@ mod tests {
817818

818819
#[test]
819820
fn test_determine_bytes_per_cluster_fat12() {
821+
assert_eq!(determine_bytes_per_cluster(128 * KB_64, 512, Some(FatType::Fat12)), 512);
820822
assert_eq!(determine_bytes_per_cluster(MB_64, 512, Some(FatType::Fat12)), 512);
821823
assert_eq!(determine_bytes_per_cluster(MB_64 + 1, 512, Some(FatType::Fat12)), 1024);
822824
assert_eq!(determine_bytes_per_cluster(MB_64, 4096, Some(FatType::Fat12)), 4096);

0 commit comments

Comments
 (0)