diff options
| author | Renato Westphal <renato@opensourcerouting.org> | 2020-10-19 23:56:54 -0300 | 
|---|---|---|
| committer | Renato Westphal <renato@opensourcerouting.org> | 2020-10-23 18:18:22 -0300 | 
| commit | 9bde0b256919ff3987ea60101229fec195324102 (patch) | |
| tree | 03d5a635efaf775dca58f389073b58c5fa2833d9 /lib/yang.c | |
| parent | 858e26d6fdd820b4943906bbe778d452e38d0198 (diff) | |
lib: fix iteration over schema nodes of a single YANG module
The only safe way to iterate over all schema nodes of a given YANG
module is by iterating over all schema nodes of all YANG modules
and filter out the nodes that belong to other modules.
The original yang_snodes_iterate_module() code did the following:
1 - Iterate over all top-level schema nodes of the given module;
2 - Iterate over all augmentations of the given module.
While that iteration strategy is more efficient, it does't handle
well more complex YANG hierarchies containing nested augmentations
or self-augmenting modules. Any iteration that isn't done on the
resolved YANG data hierarchy is fragile and prone to errors.
Fixes regression introduced by commit 8a923b48513316b where the
gen_northbound_callbacks tool was generating duplicate callbacks
for certain modules.
Signed-off-by: Renato Westphal <renato@opensourcerouting.org>
Diffstat (limited to 'lib/yang.c')
| -rw-r--r-- | lib/yang.c | 27 | 
1 files changed, 14 insertions, 13 deletions
diff --git a/lib/yang.c b/lib/yang.c index 5bf7758e18..46012acf51 100644 --- a/lib/yang.c +++ b/lib/yang.c @@ -230,22 +230,23 @@ next:  int yang_snodes_iterate_module(const struct lys_module *module,  			       yang_iterate_cb cb, uint16_t flags, void *arg)  { -	struct lys_node *snode; +	const struct lys_module *module_iter; +	uint32_t idx = 0;  	int ret = YANG_ITER_CONTINUE; -	LY_TREE_FOR (module->data, snode) { -		ret = yang_snodes_iterate_subtree(snode, module, cb, flags, -						  arg); -		if (ret == YANG_ITER_STOP) -			return ret; -	} +	idx = ly_ctx_internal_modules_count(ly_native_ctx); +	while ((module_iter = ly_ctx_get_module_iter(ly_native_ctx, &idx))) { +		struct lys_node *snode; -	for (uint8_t i = 0; i < module->augment_size; i++) { -		ret = yang_snodes_iterate_subtree( -			(const struct lys_node *)&module->augment[i], module, -			cb, flags, arg); -		if (ret == YANG_ITER_STOP) -			return ret; +		if (!module_iter->implemented) +			continue; + +		LY_TREE_FOR (module_iter->data, snode) { +			ret = yang_snodes_iterate_subtree(snode, module, cb, +							  flags, arg); +			if (ret == YANG_ITER_STOP) +				return ret; +		}  	}  	return ret;  | 
