Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix GetFullNameFromFamilyName for non-elevated context #2922

Merged
merged 2 commits into from Feb 6, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 47 additions & 8 deletions src/AppInstallerCommonCore/MsixInfo.cpp
Expand Up @@ -7,6 +7,7 @@
#include "Public/AppInstallerLogging.h"
#include "Public/AppInstallerStrings.h"
#include "Public/AppInstallerDownloader.h"
#include "Public/AppInstallerRuntime.h"

using namespace winrt::Windows::Storage::Streams;
using namespace Microsoft::WRL;
Expand Down Expand Up @@ -367,21 +368,59 @@ namespace AppInstaller::Msix
PackageManager packageManager;

std::wstring pfn = Utility::ConvertToUTF16(familyName);
auto packages = packageManager.FindPackages(pfn);
if (Runtime::IsRunningAsAdmin())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Can we add a comment about why this check is needed?

{
auto packages = packageManager.FindPackages(pfn);

std::optional<std::string> result;
for (const auto& package : packages)
std::optional<std::string> result;
for (const auto& package : packages)
{
if (result.has_value())
{
// More than 1 package found. Don't directly error, let caller deal with it.
return {};
}

result = Utility::ConvertToUTF8(package.Id().FullName());
}

return result;
}
else
{
if (result.has_value())
UINT32 fullNameCount = 0;
UINT32 bufferLength = 0;
UINT32 properties = 0;
LONG findResult = FindPackagesByPackageFamily(pfn.c_str(), PACKAGE_FILTER_HEAD, &fullNameCount, nullptr, &bufferLength, nullptr, &properties);
if (findResult == ERROR_SUCCESS || fullNameCount == 0)
{
// No package found
return {};
}
else if (findResult != ERROR_INSUFFICIENT_BUFFER)
{
THROW_WIN32(findResult);
}
else if (fullNameCount != 1)
{
// More than 1 package found. Don't directly error, let caller deal with it.
// Don't directly error, let caller deal with it
AICLI_LOG(Core, Error, << "Multiple packages found for family name: " << fullNameCount);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Can we also log something like this in the other case?

return {};
}

result = Utility::ConvertToUTF8(package.Id().FullName());
// fullNameCount == 1 at this point
PWSTR fullNamePtr;
std::wstring buffer(static_cast<size_t>(bufferLength) + 1, '\0');
THROW_IF_WIN32_ERROR(FindPackagesByPackageFamily(pfn.c_str(), PACKAGE_FILTER_HEAD, &fullNameCount, &fullNamePtr, &bufferLength, &buffer[0], &properties));
if (fullNameCount != 1 || bufferLength == 0)
{
// Something changed in between, abandon
AICLI_LOG(Core, Error, << "Packages found for family name: " << fullNameCount);
return {};
}
buffer.resize(bufferLength - 1);
return Utility::ConvertToUTF8(buffer);
}

return result;
}

std::string GetPackageFamilyNameFromFullName(std::string_view fullName)
Expand Down