Skip to content

Commit

Permalink
Fix to forbid behavior properties to be used as a value in expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
D8H committed Oct 21, 2024
1 parent a5d6584 commit 3cf3418
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
11 changes: 8 additions & 3 deletions Core/GDCore/IDE/Events/ExpressionValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,17 @@ bool ExpressionValidator::ValidateObjectVariableOrVariableOrProperty(
return true; // We found a property, even if the child is not allowed.
}

const gd::NamedPropertyDescriptor& property = propertiesContainersList.Get(identifier.identifierName).second;
const gd::NamedPropertyDescriptor &property =
propertiesContainersList.Get(identifier.identifierName).second;

if (property.GetType() == "Number") {
childType = Type::Number;
childType = Type::Number;
} else if (property.GetType() == "Boolean") {
// Nothing - we don't know the precise type (this could be used a string or as a number)
// Nothing - we don't know the precise type (this could be used a string
// or as a number)
} else if (property.GetType() == "Behavior") {
RaiseTypeError(_("Behaviors can't be used as a value in expressions."),
identifier.identifierNameLocation);
} else {
// Assume type is String or equivalent.
childType = Type::String;
Expand Down
19 changes: 19 additions & 0 deletions Core/tests/ExpressionParser2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1962,6 +1962,25 @@ TEST_CASE("ExpressionParser2", "[common][events]") {
}
}

SECTION("Invalid property (required behavior)") {
{
gd::PropertiesContainer propertiesContainer(gd::EventsFunctionsContainer::Extension);

auto projectScopedContainersWithProperties = gd::ProjectScopedContainers::MakeNewProjectScopedContainersForProjectAndLayout(project, layout1);
projectScopedContainersWithProperties.AddPropertiesContainer(propertiesContainer);

propertiesContainer.InsertNew("MyRequiredBehavior").SetType("Behavior");

auto node =
parser.ParseExpression("\"abc\" + MyRequiredBehavior");

gd::ExpressionValidator validator(platform, projectScopedContainersWithProperties, "number|string");
node->Visit(validator);
REQUIRE(validator.GetFatalErrors().size() == 1);
REQUIRE(validator.GetFatalErrors()[0]->GetMessage() == "Behaviors can't be used as a value in expressions.");
}
}

SECTION("Invalid property (unsupported child syntax, 1 level)") {
{
gd::PropertiesContainer propertiesContainer(gd::EventsFunctionsContainer::Extension);
Expand Down

0 comments on commit 3cf3418

Please sign in to comment.