Fix incorrect buffer limits in Module.FullyQualifiedName QCall (dotnet/coreclr#5753)
authorJan Kotas <jkotas@microsoft.com>
Tue, 14 Jun 2016 19:21:45 +0000 (12:21 -0700)
committerGitHub <noreply@github.com>
Tue, 14 Jun 2016 19:21:45 +0000 (12:21 -0700)
Commit migrated from https://github.com/dotnet/coreclr/commit/45c827a17a746c5caba200594021ddb38743a711

src/coreclr/src/vm/commodule.cpp

index bb3e479..44a96d3 100644 (file)
@@ -921,19 +921,15 @@ void QCALLTYPE COMModule::GetScopeName(QCall::ModuleHandle pModule, QCall::Strin
     END_QCALL;
 }
 
-static bool StringEndsWith(LPCWSTR pwzString, LPCWSTR pwzCandidate)
+static void ReplaceNiExtension(SString& fileName, PCWSTR pwzOldSuffix, PCWSTR pwzNewSuffix)
 {
-    size_t stringLength = wcslen(pwzString);
-    size_t candidateLength = wcslen(pwzCandidate);
+    STANDARD_VM_CONTRACT;
 
-    if (candidateLength > stringLength || stringLength == 0 || candidateLength == 0)
+    if (fileName.EndsWithCaseInsensitive(pwzOldSuffix))
     {
-        return false;
+        COUNT_T oldSuffixLen = (COUNT_T)wcslen(pwzOldSuffix);
+        fileName.Replace(fileName.End() - oldSuffixLen, oldSuffixLen, pwzNewSuffix);
     }
-
-    LPCWSTR pwzStringEnd = pwzString + stringLength - candidateLength;
-
-    return !_wcsicmp(pwzStringEnd, pwzCandidate);
 }
 
 /*============================GetFullyQualifiedName=============================
@@ -972,23 +968,12 @@ void QCALLTYPE COMModule::GetFullyQualifiedName(QCall::ModuleHandle pModule, QCa
                 //
                 if (pModule->GetFile()->GetAssembly()->GetILimage()->IsTrustedNativeImage())
                 {
-                    WCHAR fileNameWithoutNi[MAX_LONGPATH];
-                    
-                    wcscpy_s(fileNameWithoutNi, MAX_LONGPATH, fileName);
-                    
-                    if (StringEndsWith(fileName, W(".ni.dll")))
-                    {
-                        wcscpy_s(fileNameWithoutNi + wcslen(fileNameWithoutNi) - wcslen(W(".ni.dll")), MAX_LONGPATH, W(".dll"));
-                    }
-                    else if (StringEndsWith(fileName, W(".ni.exe")))
-                    {
-                        wcscpy_s(fileNameWithoutNi + wcslen(fileNameWithoutNi) - wcslen(W(".ni.exe")), MAX_LONGPATH, W(".exe"));
-                    }
-                    else if (StringEndsWith(fileName, W(".ni.winmd")))
-                    {
-                        wcscpy_s(fileNameWithoutNi + wcslen(fileNameWithoutNi) - wcslen(W(".ni.winmd")), MAX_LONGPATH, W(".winmd"));
-                    }
+                    SString fileNameWithoutNi(fileName);
 
+                    ReplaceNiExtension(fileNameWithoutNi, W(".ni.dll"), W(".dll"));
+                    ReplaceNiExtension(fileNameWithoutNi, W(".ni.exe"), W(".exe"));
+                    ReplaceNiExtension(fileNameWithoutNi, W(".ni.winmd"), W(".winmd"));
                     retString.Set(fileNameWithoutNi);
                 }
                 else