diff --git a/examples/gno.land/p/nt/avl/node.gno b/examples/gno.land/p/nt/avl/node.gno index 3c64ad1347e..56fe6811e48 100644 --- a/examples/gno.land/p/nt/avl/node.gno +++ b/examples/gno.land/p/nt/avl/node.gno @@ -108,12 +108,15 @@ func (node *Node) Get(key string) (index int, value any, exists bool) { // GetByIndex retrieves the key-value pair of the node at the given index // in the subtree rooted at the node. func (node *Node) GetByIndex(index int) (key string, value any) { + if index < 0 { + panic("GetByIndex: negative index not allowed") + } + if node.height == 0 { - if index == 0 { - return node.key, node.value - } else { + if index != 0 { panic("GetByIndex asked for invalid index") } + return node.key, node.value } else { // TODO: could improve this by storing the sizes leftNode := node.getLeftNode() @@ -267,6 +270,13 @@ func (node *Node) rotateLeft() *Node { func (node *Node) calcHeightAndSize() { node.height = maxInt8(node.getLeftNode().height, node.getRightNode().height) + 1 node.size = node.getLeftNode().size + node.getRightNode().size + + if node.height < 0 { + panic("height overflow: tree height exceeds int8 max") + } + if node.size < 0 { + panic("size overflow: node size exceeds int max") + } } // calcBalance calculates the balance factor of the node. diff --git a/examples/gno.land/p/nt/avl/pager/pager.gno b/examples/gno.land/p/nt/avl/pager/pager.gno index 2d277834630..8b531f55e7f 100644 --- a/examples/gno.land/p/nt/avl/pager/pager.gno +++ b/examples/gno.land/p/nt/avl/pager/pager.gno @@ -53,6 +53,10 @@ func (p *Pager) GetPage(pageNumber int) *Page { } func (p *Pager) GetPageWithSize(pageNumber, pageSize int) *Page { + if pageSize <= 0 { + panic("GetPageWithSize: invalid page size") + } + totalItems := p.Tree.Size() totalPages := int(math.Ceil(float64(totalItems) / float64(pageSize)))