Skip to content

Commit f9b48a9

Browse files
committed
device: zero out allowedip node pointers when removing
This should make it a bit easier for the garbage collector. Signed-off-by: Jason A. Donenfeld <[email protected]>
1 parent d0cf961 commit f9b48a9

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

device/allowedips.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ func (node *trieEntry) maskSelf() {
9696
}
9797
}
9898

99+
func (node *trieEntry) zeroizePointers() {
100+
// Make the garbage collector's life slightly easier
101+
node.peer = nil
102+
node.child[0] = nil
103+
node.child[1] = nil
104+
node.parent.parentBit = nil
105+
}
106+
99107
func (node *trieEntry) nodePlacement(ip net.IP, cidr uint8) (parent *trieEntry, exact bool) {
100108
for node != nil && node.cidr <= cidr && commonBits(node.bits, ip) >= node.cidr {
101109
parent = node
@@ -257,17 +265,21 @@ func (table *AllowedIPs) RemoveByPeer(peer *Peer) {
257265
}
258266
*node.parent.parentBit = child
259267
if node.child[0] != nil || node.child[1] != nil || node.parent.parentBitType > 1 {
268+
node.zeroizePointers()
260269
continue
261270
}
262271
parent := (*trieEntry)(unsafe.Pointer(uintptr(unsafe.Pointer(node.parent.parentBit)) - unsafe.Offsetof(node.child) - unsafe.Sizeof(node.child[0])*uintptr(node.parent.parentBitType)))
263272
if parent.peer != nil {
273+
node.zeroizePointers()
264274
continue
265275
}
266276
child = parent.child[node.parent.parentBitType^1]
267277
if child != nil {
268278
child.parent = parent.parent
269279
}
270280
*parent.parent.parentBit = child
281+
node.zeroizePointers()
282+
parent.zeroizePointers()
271283
}
272284
}
273285

device/allowedips_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,16 @@ func TestTrieIPv4(t *testing.T) {
159159
assertNEQ(a, 192, 0, 0, 0)
160160
assertNEQ(a, 255, 0, 0, 0)
161161

162-
allowedIPs = AllowedIPs{}
162+
allowedIPs.RemoveByPeer(a)
163+
allowedIPs.RemoveByPeer(b)
164+
allowedIPs.RemoveByPeer(c)
165+
allowedIPs.RemoveByPeer(d)
166+
allowedIPs.RemoveByPeer(e)
167+
allowedIPs.RemoveByPeer(g)
168+
allowedIPs.RemoveByPeer(h)
169+
if allowedIPs.IPv4 != nil || allowedIPs.IPv6 != nil {
170+
t.Error("Expected removing all the peers to empty trie, but it did not")
171+
}
163172

164173
insert(a, 192, 168, 0, 0, 16)
165174
insert(a, 192, 168, 0, 0, 24)

0 commit comments

Comments
 (0)