From c1327e43ab4c9281651e8505a2149a96c11371b4 Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Mon, 22 Nov 2010 21:31:00 +0000 Subject: [PATCH] [cmdline] Fix multi-layer variable expansion Expansion of ${${foo}} will currently fail, because the first opening "${" will be incorrectly matched against the first closing "}", leading to an attempt to expand the variable "${foo". Fix by ensuring that the most recent opening "${" is used to match against the first closing "}". Total cost: 8 bytes. :) Signed-off-by: Michael Brown --- src/core/exec.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/core/exec.c b/src/core/exec.c index f65a4647c..18fc57f5c 100644 --- a/src/core/exec.c +++ b/src/core/exec.c @@ -115,17 +115,21 @@ static char * expand_command ( const char *command ) { head = expcmd; - /* Locate opener */ - start = strstr ( expcmd, "${" ); - if ( ! start ) + /* Locate setting to be expanded */ + start = NULL; + end = NULL; + for ( tmp = expcmd ; *tmp ; tmp++ ) { + if ( ( tmp[0] == '$' ) && ( tmp[1] == '{' ) ) + start = tmp; + if ( tmp[0] == '}' ) + end = tmp; + if ( start && end ) + break; + } + if ( ! ( start && end ) ) break; *start = '\0'; name = ( start + 2 ); - - /* Locate closer */ - end = strstr ( name, "}" ); - if ( ! end ) - break; *end = '\0'; tail = ( end + 1 );