Skip to content
This repository
Browse code

[wiki] Update from Symbio

Fix some buffer overrun errors

Signed-off-by: Christopher Hall <hsw@openmoko.com>
  • Loading branch information...
commit 8286ec5872f718ee2348e2327880cdfaf0d64aa0 1 parent 3c198c8
Christopher Hall hxw authored

Showing 1 changed file with 6 additions and 3 deletions. Show diff stats Hide diff stats

  1. +6 3 wiki/btree.c
9 wiki/btree.c
@@ -111,7 +111,10 @@ void set_root(PBTREE btree, int node_idx)
111 111
112 112 int key_count (PBTREE btree, int node_idx)
113 113 {
114   - return btree->nodes[node_idx].element_count-1;
  114 + if (BTREE_IS_VALID_NODE_IDX(node_idx))
  115 + return btree->nodes[node_idx].element_count-1;
  116 + else
  117 + return 0;
115 118 }
116 119
117 120 PBTREE_ELEMENT largest_key (PBTREE btree, int node_idx)
@@ -275,7 +278,7 @@ int vector_insert_for_split (PBTREE btree, int node_idx, PBTREE_ELEMENT element)
275 278 memcpy(&btree->nodes[node_idx].elements[i], &btree->nodes[node_idx].elements[i-1], sizeof(BTREE_ELEMENT));
276 279 i--;
277 280 }
278   - if (element->subtree_node_idx)
  281 + if (BTREE_IS_VALID_NODE_IDX(element->subtree_node_idx))
279 282 btree->nodes[element->subtree_node_idx].parent_node_idx = node_idx;
280 283 memcpy(&btree->nodes[node_idx].elements[i], element, sizeof(BTREE_ELEMENT));
281 284 btree->nodes[node_idx].element_count++;
@@ -579,7 +582,7 @@ PBTREE_ELEMENT btree_search_ex (PBTREE btree, int *node_idx, long desired_key) {
579 582 // payload, just a subtree). the seach starts at the *this node, not
580 583 // at the root of the b-tree.
581 584 int current_node_idx = *node_idx;
582   - if (!key_count(btree, *node_idx))
  585 + if (!BTREE_IS_VALID_NODE_IDX(*node_idx) || !key_count(btree, *node_idx))
583 586 current_node_idx = BTREE_INVALID_NODE_IDX;
584 587 while (BTREE_IS_VALID_NODE_IDX(current_node_idx)) {
585 588 *node_idx = current_node_idx;

0 comments on commit 8286ec5

Please sign in to comment.
Something went wrong with that request. Please try again.