Browse Source

[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 <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 14 years ago
parent
commit
c1327e43ab
1 changed files with 12 additions and 8 deletions
  1. 12
    8
      src/core/exec.c

+ 12
- 8
src/core/exec.c View File

@@ -115,17 +115,21 @@ static char * expand_command ( const char *command ) {
115 115
 
116 116
 		head = expcmd;
117 117
 
118
-		/* Locate opener */
119
-		start = strstr ( expcmd, "${" );
120
-		if ( ! start )
118
+		/* Locate setting to be expanded */
119
+		start = NULL;
120
+		end = NULL;
121
+		for ( tmp = expcmd ; *tmp ; tmp++ ) {
122
+			if ( ( tmp[0] == '$' ) && ( tmp[1] == '{' ) )
123
+				start = tmp;
124
+			if ( tmp[0] == '}' )
125
+				end = tmp;
126
+			if ( start && end )
127
+				break;
128
+		}
129
+		if ( ! ( start && end ) )
121 130
 			break;
122 131
 		*start = '\0';
123 132
 		name = ( start + 2 );
124
-
125
-		/* Locate closer */
126
-		end = strstr ( name, "}" );
127
-		if ( ! end )
128
-			break;
129 133
 		*end = '\0';
130 134
 		tail = ( end + 1 );
131 135
 

Loading…
Cancel
Save