Skip to content

Commit 13324a0

Browse files
committed
Merge branch 'git-wrapper-interpolate'
There was a bug in the wrapper where it would interpolate incorrectly if the name of the environment variable to expand was longer than the value. Signed-off-by: Johannes Schindelin <[email protected]>
2 parents a852bd3 + d11f41d commit 13324a0

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

compat/win32/git-wrapper.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ static void extract_first_arg(LPWSTR command_line, LPWSTR exepath, LPWSTR buf)
224224
static LPWSTR expand_variables(LPWSTR buffer, size_t alloc)
225225
{
226226
LPWSTR buf = buffer;
227-
size_t len = wcslen(buf);
227+
size_t len = wcslen(buf), move_len;
228228

229229
for (;;) {
230230
LPWSTR atat = wcsstr(buf, L"@@"), atat2;
@@ -239,8 +239,9 @@ static LPWSTR expand_variables(LPWSTR buffer, size_t alloc)
239239
break;
240240

241241
*atat2 = L'\0';
242+
atat2 += 2;
242243
env_len = GetEnvironmentVariable(atat + 2, NULL, 0);
243-
delta = env_len - 1 - (atat2 + 2 - atat);
244+
delta = env_len - 1 - (atat2 - atat);
244245
if (len + delta >= alloc) {
245246
LPWSTR buf2;
246247
alloc = alloc_nr(alloc);
@@ -264,13 +265,14 @@ static LPWSTR expand_variables(LPWSTR buffer, size_t alloc)
264265
atat2 += buf2 - buf;
265266
buf = buf2;
266267
}
267-
if (delta)
268-
memmove(atat2 + 2 + delta, atat2 + 2,
269-
sizeof(WCHAR) * (len + 1
270-
- (atat2 + 2 - buf)));
268+
move_len = sizeof(WCHAR) * (len + 1 - (atat2 - buf));
269+
if (delta > 0)
270+
memmove(atat2 + delta, atat2, move_len);
271271
len += delta;
272-
save = atat[env_len - 1];
272+
save = atat[env_len - 1 + (delta < 0 ? -delta : 0)];
273273
GetEnvironmentVariable(atat + 2, atat, env_len);
274+
if (delta < 0)
275+
memmove(atat2 + delta, atat2, move_len);
274276
atat[env_len - 1] = save;
275277
}
276278

0 commit comments

Comments
 (0)