Skip to content

Commit 587339a

Browse files
authored
Add a guard against an empty range in the EXT4 formatter (#641)
Adds a guard against an empty range in the EXT4 formatter to prevent a possible crash: > Swift/arm64e-apple-macos.swiftinterface:6314: Fatal error: Range requires lowerBound <= upperBound
1 parent 586385f commit 587339a

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

Sources/ContainerizationEXT4/EXT4+Formatter.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -699,9 +699,11 @@ extension EXT4 {
699699
for i in 0...usedGroupDescriptorBlocks {
700700
bitmap[Int(i / 8)] |= 1 << (i % 8)
701701
}
702-
for i in usedGroupDescriptorBlocks + 1...self.groupDescriptorBlocks {
703-
bitmap[Int(i / 8)] &= ~(1 << (i % 8))
704-
blocks -= 1
702+
if usedGroupDescriptorBlocks + 1 <= self.groupDescriptorBlocks {
703+
for i in usedGroupDescriptorBlocks + 1...self.groupDescriptorBlocks {
704+
bitmap[Int(i / 8)] &= ~(1 << (i % 8))
705+
blocks -= 1
706+
}
705707
}
706708
}
707709

Tests/ContainerizationEXT4Tests/TestEXT4Format.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,14 @@ struct Ext4FormatTests: ~Copyable {
219219
#expect(regFile.sizeLow == 4)
220220
}
221221
}
222+
223+
struct Ext4FormatEmptyRangeTests {
224+
@Test func closeEmptyFilesystem() throws {
225+
let fsPath = FilePath(
226+
FileManager.default.temporaryDirectory
227+
.appendingPathComponent(UUID().uuidString, isDirectory: false))
228+
defer { try? FileManager.default.removeItem(at: fsPath.url) }
229+
let formatter = try EXT4.Formatter(fsPath, minDiskSize: 32.kib())
230+
try formatter.close()
231+
}
232+
}

0 commit comments

Comments
 (0)