浏览代码

Refuse to delete node #0

Signed-off-by: Vadim Markovtsev <vadim@sourced.tech>
Vadim Markovtsev 6 年之前
父节点
当前提交
55fddd58e9
共有 2 个文件被更改,包括 9 次插入0 次删除
  1. 3 0
      internal/rbtree/rbtree.go
  2. 6 0
      internal/rbtree/rbtree_test.go

+ 3 - 0
internal/rbtree/rbtree.go

@@ -272,6 +272,9 @@ func (allocator *Allocator) free(n uint32) {
 	if allocator.storage == nil {
 		panic("hibernated allocators cannot be used")
 	}
+	if n == 0 {
+		panic("node #0 is special and cannot be deallocated")
+	}
 	_, exists := allocator.gaps[n]
 	doAssert(!exists)
 	allocator.storage[n] = node{}

+ 6 - 0
internal/rbtree/rbtree_test.go

@@ -318,6 +318,12 @@ func TestRandomized(t *testing.T) {
 	}
 }
 
+func TestAllocatorFreeZero(t *testing.T) {
+	alloc := NewAllocator()
+	alloc.malloc()
+	assert.Panics(t, func(){alloc.free(0)})
+}
+
 func TestCloneShallow(t *testing.T) {
 	alloc1 := NewAllocator()
 	alloc1.malloc()