Skip to content

Commit 722bd15

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

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

compat/win32/git-wrapper.c

+25-5
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,11 @@ 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)
207+
#define alloc_nr(x) (((x)+16)*3/2)
208+
209+
static LPWSTR expand_variables(LPWSTR buffer, size_t alloc)
208210
{
211+
LPWSTR buf = buffer;
209212
size_t len = wcslen(buf);
210213

211214
for (;;) {
@@ -224,10 +227,27 @@ static LPWSTR expand_variables(LPWSTR buf, size_t alloc)
224227
env_len = GetEnvironmentVariable(atat + 2, NULL, 0);
225228
delta = env_len - 1 - (atat2 + 2 - atat);
226229
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;
231251
}
232252
if (delta)
233253
memmove(atat2 + 2 + delta, atat2 + 2,

0 commit comments

Comments
 (0)