Skip to content

Commit

Permalink
[Rope] Fix copy-on-write violation in Rope.join
Browse files Browse the repository at this point in the history
Rope.join has a logic error where it fails to properly ensure uniqueness of child node references before zipping the two b-trees together.

rdar://128450202
  • Loading branch information
lorentey committed Jun 1, 2024
1 parent 6c31865 commit a0b839d
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions Sources/RopeModule/Rope/Operations/Rope+Join.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ extension Rope {
var left = left.root
var right = right.root

left.ensureUnique()
right.ensureUnique()

if left.height >= right.height {
let r = left._graftBack(&right)
guard let remainder = r.remainder else { return Self(root: left) }
Expand All @@ -64,6 +61,10 @@ extension Rope._Node {
_ scion: inout Self
) -> (remainder: Self?, delta: Summary) {
assert(self.height >= scion.height)

self.ensureUnique()
scion.ensureUnique()

guard self.height > scion.height else {
assert(self.height == scion.height)
let d = scion.summary
Expand Down Expand Up @@ -99,6 +100,10 @@ extension Rope._Node {
_ scion: inout Self
) -> (remainder: Self?, delta: Summary) {
assert(self.height >= scion.height)

self.ensureUnique()
scion.ensureUnique()

guard self.height > scion.height else {
assert(self.height == scion.height)
let origSum = self.summary
Expand Down

0 comments on commit a0b839d

Please sign in to comment.