Skip to content

Commit 154650f

Browse files
committed
git wrapper: auto-grow buffer in expand_variables()
Signed-off-by: Johannes Schindelin <[email protected]>
1 parent c6ba885 commit 154650f

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

compat/win32/git-wrapper.c

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,11 @@ static void extract_first_arg(LPWSTR command_line, LPWSTR exepath, LPWSTR buf)
208208
LocalFree(wargv);
209209
}
210210

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)
212214
{
215+
LPWSTR buf = buffer;
213216
size_t len = wcslen(buf);
214217

215218
for (;;) {
@@ -228,10 +231,27 @@ static LPWSTR expand_variables(LPWSTR buf, size_t alloc)
228231
env_len = GetEnvironmentVariable(atat + 2, NULL, 0);
229232
delta = env_len - 1 - (atat2 + 2 - atat);
230233
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;
235255
}
236256
if (delta)
237257
memmove(atat2 + 2 + delta, atat2 + 2,

0 commit comments

Comments
 (0)