@@ -204,6 +204,44 @@ static void extract_first_arg(LPWSTR command_line, LPWSTR exepath, LPWSTR buf)
204204 LocalFree (wargv );
205205}
206206
207+ static LPWSTR expand_variables (LPWSTR buf , size_t alloc )
208+ {
209+ size_t len = wcslen (buf );
210+
211+ for (;;) {
212+ LPWSTR atat = wcsstr (buf , L"@@" ), atat2 ;
213+ WCHAR save ;
214+ int env_len , delta ;
215+
216+ if (!atat )
217+ break ;
218+
219+ atat2 = wcsstr (atat + 2 , L"@@" );
220+ if (!atat2 )
221+ break ;
222+
223+ * atat2 = L'\0' ;
224+ env_len = GetEnvironmentVariable (atat + 2 , NULL , 0 );
225+ delta = env_len - 1 - (atat2 + 2 - atat );
226+ if (len + delta >= alloc ) {
227+ fwprintf (stderr ,
228+ L"Substituting '%s' results in too "
229+ L"large a command-line\n" , atat + 2 );
230+ exit (1 );
231+ }
232+ if (delta )
233+ memmove (atat2 + 2 + delta , atat2 + 2 ,
234+ sizeof (WCHAR ) * (len + 1
235+ - (atat2 + 2 - buf )));
236+ len += delta ;
237+ save = atat [env_len - 1 ];
238+ GetEnvironmentVariable (atat + 2 , atat , env_len );
239+ atat [env_len - 1 ] = save ;
240+ }
241+
242+ return buf ;
243+ }
244+
207245static int configure_via_resource (LPWSTR basename , LPWSTR exepath , LPWSTR exep ,
208246 LPWSTR * prefix_args , int * prefix_args_len ,
209247 int * is_git_command , LPWSTR * working_directory , int * full_path ,
@@ -214,6 +252,7 @@ static int configure_via_resource(LPWSTR basename, LPWSTR exepath, LPWSTR exep,
214252
215253#define BUFSIZE 65536
216254 static WCHAR buf [BUFSIZE ];
255+ LPWSTR buf2 = buf ;
217256 int len ;
218257
219258 for (id = 0 ; ; id ++ ) {
@@ -253,48 +292,19 @@ static int configure_via_resource(LPWSTR basename, LPWSTR exepath, LPWSTR exep,
253292 if (!id )
254293 SetEnvironmentVariable (L"EXEPATH" , exepath );
255294
256- for (;;) {
257- LPWSTR atat = wcsstr (buf , L"@@" ), atat2 ;
258- WCHAR save ;
259- int env_len , delta ;
260-
261- if (!atat )
262- break ;
263-
264- atat2 = wcsstr (atat + 2 , L"@@" );
265- if (!atat2 )
266- break ;
267-
268- * atat2 = L'\0' ;
269- env_len = GetEnvironmentVariable (atat + 2 , NULL , 0 );
270- delta = env_len - 1 - (atat2 + 2 - atat );
271- if (len + delta >= BUFSIZE ) {
272- fwprintf (stderr ,
273- L"Substituting '%s' results in too "
274- L"large a command-line\n" , atat + 2 );
275- exit (1 );
276- }
277- if (delta )
278- memmove (atat2 + 2 + delta , atat2 + 2 ,
279- sizeof (WCHAR ) * (len + 1
280- - (atat2 + 2 - buf )));
281- len += delta ;
282- save = atat [env_len - 1 ];
283- GetEnvironmentVariable (atat + 2 , atat , env_len );
284- atat [env_len - 1 ] = save ;
285- }
295+ buf2 = expand_variables (buf , BUFSIZE );
286296
287- extract_first_arg (buf , exepath , exep );
297+ extract_first_arg (buf2 , exepath , exep );
288298
289299 if (_waccess (exep , 0 ) != -1 )
290300 break ;
291301 fwprintf (stderr ,
292302 L"Skipping command-line '%s'\n('%s' not found)\n" ,
293- buf , exep );
303+ buf2 , exep );
294304 }
295305
296- * prefix_args = buf ;
297- * prefix_args_len = wcslen (buf );
306+ * prefix_args = buf2 ;
307+ * prefix_args_len = wcslen (buf2 );
298308
299309 * is_git_command = 0 ;
300310 * working_directory = (LPWSTR ) 1 ;
0 commit comments