Skip to content

Commit

Permalink
Add validation for progressReporter and improve map insertion
Browse files Browse the repository at this point in the history
- Added validation to ensure that the `progressReporter` function is not null in the constructors of `CompletionOnlyProgressSink` and `PreIndexedPackageCatalogProgressSink`.
   - If `progressReporter` is null, an `E_INVALIDARG` exception is thrown.

- Modified the insertion of the default weight for `AppInstaller::ProgressType::Percent` in `PreIndexedPackageCatalogProgressSink` to use `insert_or_assign` instead of direct assignment for better robustness.
  • Loading branch information
Madhusudhan-MSFT committed Oct 24, 2024
1 parent 2066dbf commit 16ecf85
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Microsoft.Management.Deployment/PackageCatalogProgress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ namespace winrt::Microsoft::Management::Deployment
CompletionOnlyProgressSink::CompletionOnlyProgressSink(std::function<void(double)> progressReporter) :
m_progressReporter(progressReporter)
{
if (!m_progressReporter)
{
THROW_HR(E_INVALIDARG);
}
}

void CompletionOnlyProgressSink::OnProgress(uint64_t current, uint64_t maximum, AppInstaller::ProgressType type)
Expand Down Expand Up @@ -74,10 +78,15 @@ namespace winrt::Microsoft::Management::Deployment
PreIndexedPackageCatalogProgressSink::PreIndexedPackageCatalogProgressSink(std::unordered_map<AppInstaller::ProgressType, double> progressWeights, std::function<void(double)> progressReporter) :
m_progressWeights(progressWeights), m_progressReporter(progressReporter)
{
if (!m_progressReporter)
{
THROW_HR(E_INVALIDARG);
}

// If no weights are provided, default to percent.
if (m_progressWeights.empty())
{
m_progressWeights[AppInstaller::ProgressType::Percent] = 1;
m_progressWeights.insert_or_assign(AppInstaller::ProgressType::Percent, 1.0);
}

// Calculate the total weight.
Expand Down

0 comments on commit 16ecf85

Please sign in to comment.