Skip to content

Commit 3ce5c70

Browse files
committed
1 parent e3e5290 commit 3ce5c70

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

Sources/ContainerizationEXT4/EXT4+Ptr.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,22 @@ extension EXT4 {
5050
self.initialized = true
5151
}
5252

53+
// Bug #15 (HIGH, 2 parts): (a) deallocate() called underlying.deallocate() without first calling
54+
// deinitialize(), leaking ARC-managed values (e.g. FileTreeNode.children Array) held in T.
55+
// (b) move() had no guard, allowing it to be called on uninitialized or deallocated memory,
56+
// causing undefined behaviour. Fixed by calling deinitialize in deallocate() and adding
57+
// an allocated && initialized guard to move().
58+
// Same fix: sonnet, sonnet-1m, sonnet-fix. All other branches leak ARC values on dealloc.
5359
func deallocate() {
5460
guard self.allocated else {
5561
return
5662
}
63+
if self.initialized {
64+
self.underlying.deinitialize(count: self.capacity)
65+
self.initialized = false
66+
}
5767
self.underlying.deallocate()
5868
self.allocated = false
59-
self.initialized = false
6069
}
6170

6271
func deinitialize(count: Int) {
@@ -72,8 +81,11 @@ extension EXT4 {
7281
}
7382

7483
func move() -> T {
84+
// Bug #15
85+
guard self.allocated && self.initialized else {
86+
fatalError("move() called on an uninitialized or deallocated Ptr")
87+
}
7588
self.initialized = false
76-
self.allocated = true
7789
return self.underlying.move()
7890
}
7991

0 commit comments

Comments
 (0)