From 4a481177541123162e5b6dbbbb5d4ec1305b2b83 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Sun, 1 Nov 2015 18:15:30 +0100 Subject: [PATCH 1/3] git-wrapper: simplify interpolation code After we found the `@@` marker after the key to interpolate, we pretty much only need the offset *after* the marker. So let's just advance it instead of adding 2 in many places. Signed-off-by: Johannes Schindelin --- compat/win32/git-wrapper.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/compat/win32/git-wrapper.c b/compat/win32/git-wrapper.c index 7cb9cf51b8..65a0001c26 100644 --- a/compat/win32/git-wrapper.c +++ b/compat/win32/git-wrapper.c @@ -239,8 +239,9 @@ static LPWSTR expand_variables(LPWSTR buffer, size_t alloc) break; *atat2 = L'\0'; + atat2 += 2; env_len = GetEnvironmentVariable(atat + 2, NULL, 0); - delta = env_len - 1 - (atat2 + 2 - atat); + delta = env_len - 1 - (atat2 - atat); if (len + delta >= alloc) { LPWSTR buf2; alloc = alloc_nr(alloc); @@ -264,10 +265,10 @@ static LPWSTR expand_variables(LPWSTR buffer, size_t alloc) atat2 += buf2 - buf; buf = buf2; } - if (delta) - memmove(atat2 + 2 + delta, atat2 + 2, + if (delta > 0) + memmove(atat2 + delta, atat2, sizeof(WCHAR) * (len + 1 - - (atat2 + 2 - buf))); + - (atat2 - buf))); len += delta; save = atat[env_len - 1]; GetEnvironmentVariable(atat + 2, atat, env_len); From da8a6fa5d2c6d35859b53fd1cf8e90e46836a3fb Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Sun, 1 Nov 2015 18:26:16 +0100 Subject: [PATCH 2/3] git-wrapper: make the interpolation code easier to understand When moving bytes (because the name and the value of the environment variable to interpolate differ in length), we introduce a variable to unclutter the code and make it more obvious what is happening. Signed-off-by: Johannes Schindelin --- compat/win32/git-wrapper.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/compat/win32/git-wrapper.c b/compat/win32/git-wrapper.c index 65a0001c26..3b2791b36c 100644 --- a/compat/win32/git-wrapper.c +++ b/compat/win32/git-wrapper.c @@ -224,7 +224,7 @@ static void extract_first_arg(LPWSTR command_line, LPWSTR exepath, LPWSTR buf) static LPWSTR expand_variables(LPWSTR buffer, size_t alloc) { LPWSTR buf = buffer; - size_t len = wcslen(buf); + size_t len = wcslen(buf), move_len; for (;;) { LPWSTR atat = wcsstr(buf, L"@@"), atat2; @@ -265,10 +265,9 @@ static LPWSTR expand_variables(LPWSTR buffer, size_t alloc) atat2 += buf2 - buf; buf = buf2; } + move_len = sizeof(WCHAR) * (len + 1 - (atat2 - buf)); if (delta > 0) - memmove(atat2 + delta, atat2, - sizeof(WCHAR) * (len + 1 - - (atat2 - buf))); + memmove(atat2 + delta, atat2, move_len); len += delta; save = atat[env_len - 1]; GetEnvironmentVariable(atat + 2, atat, env_len); From 7eb88c150b57eb285c9f931aa9c3dcaa3cead184 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Sun, 1 Nov 2015 18:29:13 +0100 Subject: [PATCH 3/3] git-wrapper: fix interpolation with short values To be precise: when the value of the environment variable is shorter than its name, we have to move the remaining bytes *after* expanding the environment variable: we would look for the wrong name otherwise. When the value is longer than the name, we still need to move the bytes out of the way first, to avoid overwriting them with the interpolated text. This fixes https://github.com/git-for-windows/git/issues/509 Signed-off-by: Johannes Schindelin --- compat/win32/git-wrapper.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/compat/win32/git-wrapper.c b/compat/win32/git-wrapper.c index 3b2791b36c..11a574646a 100644 --- a/compat/win32/git-wrapper.c +++ b/compat/win32/git-wrapper.c @@ -269,8 +269,10 @@ static LPWSTR expand_variables(LPWSTR buffer, size_t alloc) if (delta > 0) memmove(atat2 + delta, atat2, move_len); len += delta; - save = atat[env_len - 1]; + save = atat[env_len - 1 + (delta < 0 ? -delta : 0)]; GetEnvironmentVariable(atat + 2, atat, env_len); + if (delta < 0) + memmove(atat2 + delta, atat2, move_len); atat[env_len - 1] = save; }