@@ -208,8 +208,11 @@ static void extract_first_arg(LPWSTR command_line, LPWSTR exepath, LPWSTR buf)
208
208
LocalFree (wargv );
209
209
}
210
210
211
- static LPWSTR expand_variables (LPWSTR buf , size_t alloc )
211
+ #define alloc_nr (x ) (((x)+16)*3/2)
212
+
213
+ static LPWSTR expand_variables (LPWSTR buffer , size_t alloc )
212
214
{
215
+ LPWSTR buf = buffer ;
213
216
size_t len = wcslen (buf );
214
217
215
218
for (;;) {
@@ -228,10 +231,27 @@ static LPWSTR expand_variables(LPWSTR buf, size_t alloc)
228
231
env_len = GetEnvironmentVariable (atat + 2 , NULL , 0 );
229
232
delta = env_len - 1 - (atat2 + 2 - atat );
230
233
if (len + delta >= alloc ) {
231
- fwprintf (stderr ,
232
- L"Substituting '%s' results in too "
233
- L"large a command-line\n" , atat + 2 );
234
- exit (1 );
234
+ LPWSTR buf2 ;
235
+ alloc = alloc_nr (alloc );
236
+ if (alloc <= len + delta )
237
+ alloc = len + delta + 1 ;
238
+ if (buf != buffer )
239
+ buf2 = realloc (buf , sizeof (WCHAR ) * alloc );
240
+ else {
241
+ buf2 = malloc (sizeof (WCHAR ) * alloc );
242
+ if (buf2 )
243
+ memcpy (buf2 , buf , sizeof (WCHAR )
244
+ * (len + 1 ));
245
+ }
246
+ if (!buf2 ) {
247
+ fwprintf (stderr ,
248
+ L"Substituting '%s' results in too "
249
+ L"large a command-line\n" , atat + 2 );
250
+ exit (1 );
251
+ }
252
+ atat += buf2 - buf ;
253
+ atat2 += buf2 - buf ;
254
+ buf = buf2 ;
235
255
}
236
256
if (delta )
237
257
memmove (atat2 + 2 + delta , atat2 + 2 ,
0 commit comments