Skip to content

Commit

Permalink
Trim Version When Parsing (#4554)
Browse files Browse the repository at this point in the history
When parsing a version, it may be possible that a publisher included one
or more whitespace characters at the end of the DisplayVersion key.
Since a folder name cannot end with a whitespace character, this means
that any PackageVersion in the community repository cannot have a
trailing whitespace character.

Due to the way versions are parsed, the trailing whitespace gets
considered as an "other" of the version part during the version
comparison. This means that `5.8 ` is considered a lower version than
`5.8` when parsed.

This PR makes it so that when a version is assigned, as is the case when
parsing the installed versions, both the final version and the version
parts are trimmed of whitespace. This means that `5.0.13` is now
considered equal to `5.0 .13`, `5.0.13 `, or even ` 5 . 0 .13 `
  • Loading branch information
Trenly committed Jun 14, 2024
1 parent 2e01798 commit 49d1a22
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/AppInstallerSharedLib/Versions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace AppInstaller::Utility

void Version::Assign(std::string version, std::string_view splitChars)
{
m_version = std::move(version);
m_version = std::move(Utility::Trim(version));

// Process approximate comparator if applicable
std::string baseVersion = m_version;
Expand Down Expand Up @@ -281,17 +281,18 @@ namespace AppInstaller::Utility

Version::Part::Part(const std::string& part)
{
const char* begin = part.c_str();
std::string interimPart = Utility::Trim(part.c_str());
const char* begin = interimPart.c_str();
char* end = nullptr;
errno = 0;
Integer = strtoull(begin, &end, 10);

if (errno == ERANGE)
{
Integer = 0;
Other = part;
Other = interimPart;
}
else if (static_cast<size_t>(end - begin) != part.length())
else if (static_cast<size_t>(end - begin) != interimPart.length())
{
Other = end;
}
Expand All @@ -300,7 +301,7 @@ namespace AppInstaller::Utility
}

Version::Part::Part(uint64_t integer, std::string other) :
Integer(integer), Other(std::move(other))
Integer(integer), Other(std::move(Utility::Trim(other)))
{
m_foldedOther = Utility::FoldCase(static_cast<std::string_view>(Other));
}
Expand Down Expand Up @@ -468,7 +469,7 @@ namespace AppInstaller::Utility
THROW_HR_IF(E_INVALIDARG, splitChars != DefaultSplitChars);

// First split off any trailing build metadata
std::string interimVersion = version;
std::string interimVersion = Utility::Trim(version);
size_t buildMetadataPos = interimVersion.find('+', 0);

if (buildMetadataPos != std::string::npos)
Expand Down

0 comments on commit 49d1a22

Please sign in to comment.