[Asterisk-code-review] config.c: Make ast_variable_retrieve return last match. (asterisk[master])

N A asteriskteam at digium.com
Wed Dec 21 15:30:45 CST 2022


N A has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/19744 )


Change subject: config.c: Make ast_variable_retrieve return last match.
......................................................................

config.c: Make ast_variable_retrieve return last match.

ast_variable_retrieve currently returns the first match
for a variable, as opposed to the last one. This is problematic
because modules that load config settings by explicitly
calling ast_variable_retrieve on a variable name (as opposed
to iterating through all the directives as specified) will
end up taking the first specified value, such as the default
value from the template rather than the actual effective value
in an individual config section, leading to the wrong config.

This fixes this by making ast_variable_retrieve return the last
match, or the most recently overridden one, as the effective setting.
This is similar to what the -1 index in the AST_CONFIG function does.

There is another function, ast_variable_find_last_in_list, that does
something similar. However, it's a slightly different API, and it
sees virtually no usage in Asterisk. ast_variable_retrieve is what
most things use so this is currently the relevant point of breakage.

ASTERISK-30370 #close

Change-Id: Ia681407275a557c1462f93832a4d45f31c580354
---
M main/config.c
1 file changed, 40 insertions(+), 3 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/44/19744/1

diff --git a/main/config.c b/main/config.c
index 1074407..b7ecd81 100644
--- a/main/config.c
+++ b/main/config.c
@@ -783,11 +783,19 @@
 const char *ast_variable_retrieve(struct ast_config *config, const char *category, const char *variable)
 {
 	struct ast_variable *v;
+	const char *match = NULL;
+
+	/* We can't return as soon as we find a match, because if a config section overrides
+	 * something specified in a template, then the actual effective value is the last
+	 * one encountered, not the first one.
+	 * (This is like using the -1 index for the AST_CONFIG function.)
+	 * Also see ast_variable_find_last_in_list
+	 */
 
 	if (category) {
 		for (v = ast_variable_browse(config, category); v; v = v->next) {
 			if (!strcasecmp(variable, v->name)) {
-				return v->value;
+				match = v->value;
 			}
 		}
 	} else {
@@ -796,13 +804,13 @@
 		for (cat = config->root; cat; cat = cat->next) {
 			for (v = cat->root; v; v = v->next) {
 				if (!strcasecmp(variable, v->name)) {
-					return v->value;
+					match = v->value;
 				}
 			}
 		}
 	}
 
-	return NULL;
+	return match;
 }
 
 const char *ast_variable_retrieve_filtered(struct ast_config *config,

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/19744
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Change-Id: Ia681407275a557c1462f93832a4d45f31c580354
Gerrit-Change-Number: 19744
Gerrit-PatchSet: 1
Gerrit-Owner: N A <asterisk at phreaknet.org>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20221221/a521589f/attachment.html>


More information about the asterisk-code-review mailing list