Skip to content

Commit

Permalink
Don't process (append) environment variable twice (ref gh-724).
Browse files Browse the repository at this point in the history
  Example: `PATH=%ConEmuBaseDir%\Scripts;%PATH%;C:\Tools\Arc`
  So, do not add `%ConEmuBaseDir%\Scripts;` and `;C:\Tools\Arc`
  if they already exist in PATH. Only explicit comparison is done,
  so, if `C:\Tools\Arc` is in the middle of current `%PATH%`,
  it would be processed (appended).
  • Loading branch information
Maximus5 committed Jun 13, 2016
1 parent cb245c5 commit a9ea7d7
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/ConEmuCD/StartEnv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,62 @@ void CStartEnv::Echo(LPCWSTR asSwitches, LPCWSTR asText)

void CStartEnv::Set(LPCWSTR asName, LPCWSTR asValue)
{
// Validate the name
if (!asName || !*asName)
{
_ASSERTE(asName && *asName);
return;
}

// Don't append variable twice
if (asValue && *asValue && wcschr(asValue, L'%'))
{
CEStr lsCurValue(GetEnvVar(asName));
if (!lsCurValue.IsEmpty())
{
// Example: PATH=%ConEmuBaseDir%\Scripts;%PATH%;C:\Tools\Arc
CEStr lsSelfName(L"%", asName, L"%");
wchar_t* pchName = lsSelfName.IsEmpty() ? NULL : StrStrI(asValue, lsSelfName);
if (pchName)
{
bool bDiffFound = false;
int iCmp;
INT_PTR iSelfLen = lsSelfName.GetLen();
INT_PTR iCurLen = lsCurValue.GetLen();
// Prefix
if (pchName > asValue)
{
CEStr lsNewPrefix;
lsNewPrefix.Set(asValue, (pchName - asValue));
INT_PTR iPLen = lsNewPrefix.GetLen();
_ASSERTE(iPLen == (pchName - asValue));
iCmp = (iCurLen >= iPLen && iPLen > 0)
? wcsncmp(lsCurValue, lsNewPrefix, iPLen)
: -1;
if (iCmp != 0)
bDiffFound = true;
}
// Suffix
if (pchName[iSelfLen])
{
CEStr lsNewSuffix;
lsNewSuffix.Set(pchName+iSelfLen);
INT_PTR iSLen = lsNewSuffix.GetLen();
iCmp = (iCurLen >= iSLen && iSLen > 0)
? wcsncmp(lsCurValue.c_str() + iCurLen - iSLen, lsNewSuffix, iSLen)
: -1;
if (iCmp != 0)
bDiffFound = true;
}
if (!bDiffFound)
{
// Nothing new to append
return;
}
}
}
}

// Expand value
wchar_t* pszExpanded = ExpandEnvStr(asValue);
LPCWSTR pszSet = pszExpanded ? pszExpanded : asValue;
Expand Down

0 comments on commit a9ea7d7

Please sign in to comment.