Fix #10438: [GS] Validate story page element type for ScriptStoryPage::NewElement (#11888)

Fixes a crash that happens if an invalid StoryPageElementType is passed to ScriptStoryPage::NewElement.

Adds an enforced precondition that tests the validity of StoryPageElementType.

Adds GSStoryPage::IsValidStoryPageElementType to the API.
This commit is contained in:
SamuXarick
2024-01-27 21:11:02 +00:00
committed by GitHub
parent 6d276698b6
commit 76f0f9e386
3 changed files with 15 additions and 0 deletions

View File

@@ -38,6 +38,11 @@ static inline bool StoryPageElementTypeRequiresText(StoryPageElementType type)
return ::StoryPageElement::IsValidID(story_page_element_id);
}
/* static */ bool ScriptStoryPage::IsValidStoryPageElementType(StoryPageElementType type)
{
return type == SPET_TEXT || type == SPET_LOCATION || type == SPET_GOAL || type == SPET_BUTTON_PUSH || type == SPET_BUTTON_TILE || type == SPET_BUTTON_VEHICLE;
}
/* static */ ScriptStoryPage::StoryPageID ScriptStoryPage::New(ScriptCompany::CompanyID company, Text *title)
{
CCountedPtr<Text> counter(title);
@@ -63,6 +68,7 @@ static inline bool StoryPageElementTypeRequiresText(StoryPageElementType type)
EnforceDeityMode(STORY_PAGE_ELEMENT_INVALID);
EnforcePrecondition(STORY_PAGE_ELEMENT_INVALID, IsValidStoryPage(story_page_id));
EnforcePrecondition(STORY_PAGE_ELEMENT_INVALID, IsValidStoryPageElementType(type));
std::string encoded_text;
if (StoryPageElementTypeRequiresText(btype)) {
EnforcePrecondition(STORY_PAGE_ELEMENT_INVALID, text != nullptr);