From c5d8525fc7d5dc5a53c06a8daa633e19d89f0a67 Mon Sep 17 00:00:00 2001 From: Dmitry Kovba Date: Thu, 2 Apr 2026 22:32:15 -0700 Subject: [PATCH 1/2] Add a guard against an empty range --- Sources/ContainerizationEXT4/EXT4+Formatter.swift | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Sources/ContainerizationEXT4/EXT4+Formatter.swift b/Sources/ContainerizationEXT4/EXT4+Formatter.swift index f386537a..045e9cd0 100644 --- a/Sources/ContainerizationEXT4/EXT4+Formatter.swift +++ b/Sources/ContainerizationEXT4/EXT4+Formatter.swift @@ -699,9 +699,11 @@ extension EXT4 { for i in 0...usedGroupDescriptorBlocks { bitmap[Int(i / 8)] |= 1 << (i % 8) } - for i in usedGroupDescriptorBlocks + 1...self.groupDescriptorBlocks { - bitmap[Int(i / 8)] &= ~(1 << (i % 8)) - blocks -= 1 + if usedGroupDescriptorBlocks + 1 <= self.groupDescriptorBlocks { + for i in usedGroupDescriptorBlocks + 1...self.groupDescriptorBlocks { + bitmap[Int(i / 8)] &= ~(1 << (i % 8)) + blocks -= 1 + } } } From fad35be3a68f2b0d0eb24c7e1f105e789168e8d5 Mon Sep 17 00:00:00 2001 From: Dmitry Kovba Date: Fri, 3 Apr 2026 14:50:51 -0700 Subject: [PATCH 2/2] Add a test closing an empty file system --- Tests/ContainerizationEXT4Tests/TestEXT4Format.swift | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Tests/ContainerizationEXT4Tests/TestEXT4Format.swift b/Tests/ContainerizationEXT4Tests/TestEXT4Format.swift index 4c0f2952..c9796c63 100644 --- a/Tests/ContainerizationEXT4Tests/TestEXT4Format.swift +++ b/Tests/ContainerizationEXT4Tests/TestEXT4Format.swift @@ -219,3 +219,14 @@ struct Ext4FormatTests: ~Copyable { #expect(regFile.sizeLow == 4) } } + +struct Ext4FormatEmptyRangeTests { + @Test func closeEmptyFilesystem() throws { + let fsPath = FilePath( + FileManager.default.temporaryDirectory + .appendingPathComponent(UUID().uuidString, isDirectory: false)) + defer { try? FileManager.default.removeItem(at: fsPath.url) } + let formatter = try EXT4.Formatter(fsPath, minDiskSize: 32.kib()) + try formatter.close() + } +}