commit 473023652e0746cdd1f260fdb9922f7a541bce55
parent d85a0f7264fdc9915e857d550284603c4902fede
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Wed, 8 Apr 2026 10:00:54 +0200
Fix for the sln_node_get_child_count function
The internal function node_child_count, on which the
sln_node_get_child_count function relies, assumes that the node being
queried is an internal node and may therefore throw an exception if the
provided node is a leaf. However, verifying that the node passed to
node_child_count is not a leaf is not straightforward, as this node
may be partially configured, as is the case during tree construction.
Calling sln_node_is_leaf would therefore not suffice.
Consequently, the sln_node_get_child_count function now first checks
whether the input node is a leaf before, if applicable, calculating its
number of children. Otherwise, the number of children is simply 0.
In addition, the type of the number of children is now an unsigned,
rather than a size_t, as this will be more than sufficient.
Furthermore, this type is now consistent with that of the tree's arity.
Diffstat:
3 files changed, 16 insertions(+), 9 deletions(-)
diff --git a/src/sln.h b/src/sln.h
@@ -302,7 +302,7 @@ SLN_API int
sln_node_is_leaf
(const struct sln_node* node);
-SLN_API size_t
+SLN_API unsigned
sln_node_get_child_count
(const struct sln_tree* tree,
const struct sln_node* node);
diff --git a/src/sln_tree.c b/src/sln_tree.c
@@ -386,7 +386,7 @@ release_tree(ref_T* ref)
/*******************************************************************************
* Local function
******************************************************************************/
-size_t
+unsigned
node_child_count
(const struct sln_tree* tree,
const size_t inode,
@@ -420,7 +420,9 @@ node_child_count
ASSERT(nchildren >= 2);
if(out_nlines_per_child) *out_nlines_per_child = nlines_per_child;
- return nchildren;
+
+ ASSERT(nchildren <= UINT_MAX);
+ return (unsigned)nchildren;
}
/*******************************************************************************
@@ -639,17 +641,21 @@ sln_node_is_leaf(const struct sln_node* node)
return node->offset == 0;
}
-size_t
+unsigned
sln_node_get_child_count
(const struct sln_tree* tree,
const struct sln_node* node)
{
- size_t inode = 0;
ASSERT(tree && node);
- inode = (size_t)(node - darray_node_cdata_get(&tree->nodes));
- ASSERT(inode < darray_node_size_get(&tree->nodes));
- return node_child_count(tree, inode, NULL);
+ if(sln_node_is_leaf(node)) {
+ return 0; /* No child */
+
+ } else {
+ const size_t inode = (size_t)(node - darray_node_cdata_get(&tree->nodes));
+ ASSERT(inode < darray_node_size_get(&tree->nodes));
+ return node_child_count(tree, inode, NULL);
+ }
}
const struct sln_node*
diff --git a/src/sln_tree_c.h b/src/sln_tree_c.h
@@ -73,7 +73,8 @@ extern LOCAL_SYM res_T
tree_build
(struct sln_tree* tree);
-extern LOCAL_SYM size_t
+/* Assume that the node is an internal node */
+extern LOCAL_SYM unsigned
node_child_count
(const struct sln_tree* tree,
const size_t inode,