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