Skip to content

Commit

Permalink
Disallow invalid triplet names passed via --triplet (#1474)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas1664 authored Sep 17, 2024
1 parent 2488bd6 commit e2f895e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/vcpkg-test/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,18 @@ See )" + docs::triplets_url + R"( for more information.
REQUIRE(maybe_check.error() == expected_error);
}

TEST_CASE ("check_triplet rejects malformed triplet", "[input][check_triplet]")
{
TripletDatabase db;
db.available_triplets.push_back(TripletFile{"invalid.triplet_name", "invalid.triplet_name.cmake"});
auto maybe_check = check_triplet("invalid.triplet_name", db);
REQUIRE(!maybe_check.has_value());
static constexpr StringLiteral expected_error{
"error: expected the end of input parsing a package spec; this usually means the indicated character is not "
"allowed to be in a package spec. Port, triplet, and feature names are all lowercase alphanumeric+hypens."};
REQUIRE(maybe_check.error() == expected_error);
}

TEST_CASE ("check_and_get_package_spec validates the triplet", "[input][check_and_get_package_spec]")
{
TripletDatabase db;
Expand Down
8 changes: 7 additions & 1 deletion src/vcpkg/input.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

#include <vcpkg/base/checks.h>
#include <vcpkg/base/messages.h>
#include <vcpkg/base/parse.h>
#include <vcpkg/base/strings.h>

#include <vcpkg/commands.help.h>
Expand All @@ -26,6 +26,12 @@ namespace vcpkg
{
// Intentionally show the lowercased string
auto as_lower = Strings::ascii_to_lowercase(name);

if (std::find_if_not(name.begin(), name.end(), ParserBase::is_package_name_char) != name.end())
{
return msg::format_error(msgParseQualifiedSpecifierNotEof);
}

if (!database.is_valid_triplet_canonical_name(as_lower))
{
LocalizedString result = msg::format_error(msgInvalidTriplet, msg::triplet = as_lower);
Expand Down

0 comments on commit e2f895e

Please sign in to comment.