Bug 1643199 Followup - Properly account for registry data length. r=agashlin,bytesized

Differential Revision: https://phabricator.services.mozilla.com/D83842
This commit is contained in:
Molly Howell 2020-07-17 16:02:16 +00:00
Родитель e0e7cf64cc
Коммит 8e5010f87c
1 изменённых файлов: 34 добавлений и 19 удалений

Просмотреть файл

@ -262,6 +262,9 @@ bool PathContainsInvalidLinks(wchar_t* const fullPath) {
bool IsProgramFilesPath(NS_tchar* fullPath) { bool IsProgramFilesPath(NS_tchar* fullPath) {
// Make sure we don't try to compare against a short path. // Make sure we don't try to compare against a short path.
DWORD longInstallPathChars = GetLongPathNameW(fullPath, nullptr, 0); DWORD longInstallPathChars = GetLongPathNameW(fullPath, nullptr, 0);
if (longInstallPathChars == 0) {
return false;
}
mozilla::UniquePtr<wchar_t[]> longInstallPath = mozilla::UniquePtr<wchar_t[]> longInstallPath =
mozilla::MakeUnique<wchar_t[]>(longInstallPathChars); mozilla::MakeUnique<wchar_t[]>(longInstallPathChars);
if (!longInstallPath || !GetLongPathNameW(fullPath, longInstallPath.get(), if (!longInstallPath || !GetLongPathNameW(fullPath, longInstallPath.get(),
@ -290,7 +293,7 @@ bool IsProgramFilesPath(NS_tchar* fullPath) {
return false; return false;
} }
if (programFiles32Path.get()[length - 1] == L'\\') { if (programFiles32Path.get()[length - 1] == L'\\') {
if (wcsncmp(longInstallPath.get(), programFiles32Path.get(), length) == if (wcsnicmp(longInstallPath.get(), programFiles32Path.get(), length) ==
0) { 0) {
return true; return true;
} }
@ -307,8 +310,8 @@ bool IsProgramFilesPath(NS_tchar* fullPath) {
NS_tsnprintf(programFiles32PathWithSlash.get(), length + 1, NS_T("%s\\"), NS_tsnprintf(programFiles32PathWithSlash.get(), length + 1, NS_T("%s\\"),
programFiles32Path.get()); programFiles32Path.get());
if (wcsncmp(longInstallPath.get(), programFiles32PathWithSlash.get(), if (wcsnicmp(longInstallPath.get(), programFiles32PathWithSlash.get(),
length) == 0) { length) == 0) {
return true; return true;
} }
} }
@ -329,15 +332,14 @@ bool IsProgramFilesPath(NS_tchar* fullPath) {
nullptr, nullptr, &length) != ERROR_SUCCESS) { nullptr, nullptr, &length) != ERROR_SUCCESS) {
return false; return false;
} }
// RegGetValue returns the length including the terminator in bytes, so // RegGetValue returns the length including the terminator, but it's in
// convert that to characters and then add one more in case we have to // bytes, so convert that to characters.
// append a trailing slash.
DWORD lengthChars = (length / sizeof(wchar_t)); DWORD lengthChars = (length / sizeof(wchar_t));
if (lengthChars <= 1) { if (lengthChars <= 1) {
return false; return false;
} }
mozilla::UniquePtr<wchar_t[]> programFilesNativePath = mozilla::UniquePtr<wchar_t[]> programFilesNativePath =
mozilla::MakeUnique<wchar_t[]>(lengthChars + 1); mozilla::MakeUnique<wchar_t[]>(lengthChars);
if (!programFilesNativePath) { if (!programFilesNativePath) {
return false; return false;
} }
@ -349,22 +351,35 @@ bool IsProgramFilesPath(NS_tchar* fullPath) {
programFilesNativePath.get(), &length) != ERROR_SUCCESS) { programFilesNativePath.get(), &length) != ERROR_SUCCESS) {
return false; return false;
} }
if (wcslen(programFilesNativePath.get()) != lengthChars - 1) { size_t nativePathStrLen =
// Something is very wrong if we didn't read all the characters that the wcsnlen_s(programFilesNativePath.get(), lengthChars);
// registry told us to expect. if (nativePathStrLen == 0) {
return false; return false;
} }
// As before, append a backslash if there isn't one already, and also add // As before, append a backslash if there isn't one already.
// that character to number we need to compare. if (programFilesNativePath.get()[nativePathStrLen - 1] == L'\\') {
if (programFilesNativePath[lengthChars - 2] != L'\\') { if (wcsnicmp(longInstallPath.get(), programFilesNativePath.get(),
wcsncat_s(programFilesNativePath.get(), lengthChars, L"\\", 1); nativePathStrLen) == 0) {
lengthChars++; return true;
} }
} else {
// Allocate space for a copy of the string along with a terminator and one
// extra character for the trailing backslash.
nativePathStrLen += 1;
mozilla::UniquePtr<wchar_t[]> programFilesNativePathWithSlash =
mozilla::MakeUnique<wchar_t[]>(nativePathStrLen + 1);
if (!programFilesNativePathWithSlash) {
return false;
}
if (wcsncmp(longInstallPath.get(), programFilesNativePath.get(), NS_tsnprintf(programFilesNativePathWithSlash.get(), nativePathStrLen + 1,
lengthChars - 1) == 0) { NS_T("%s\\"), programFilesNativePath.get());
return true;
if (wcsnicmp(longInstallPath.get(), programFilesNativePathWithSlash.get(),
nativePathStrLen) == 0) {
return true;
}
} }
} }