From b82567ebd31b08d5020cd07c7a0d2ebb2d0aa32f Mon Sep 17 00:00:00 2001 From: Ghom <42542238+Ghommie@users.noreply.github.com> Date: Sat, 1 Jul 2023 20:21:07 +0200 Subject: [PATCH] AddComponent() now throws a crash message when the comp type arg is not a component. (#76221) ## About The Pull Request HEY! Did you know that the AddComponent() proc can complete without throwing errors when the component type argument is actually not a type or instance of a component? Me neither (until now), but I've recently discovered that while working on #76219 (Make sure to merge that one first). Also adds early return checks to the AddElement and RemoveElement procs. ## Why It's Good For The Game Silent bugs are awful. ## Changelog N/A --- code/datums/components/_component.dm | 19 +++++++++++-------- code/datums/elements/_element.dm | 4 ++++ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/code/datums/components/_component.dm b/code/datums/components/_component.dm index 26d562a824a..a99379db7e8 100644 --- a/code/datums/components/_component.dm +++ b/code/datums/components/_component.dm @@ -317,6 +317,17 @@ if(QDELING(src)) CRASH("Attempted to add a new component of type \[[component_type]\] to a qdeleting parent of type \[[type]\]!") + var/datum/component/new_component + + if(!ispath(component_type, /datum/component)) + if(!istype(component_type, /datum/component)) + CRASH("Attempted to instantiate \[[component_type]\] as a component added to parent of type \[[type]\]!") + else + new_component = component_type + component_type = new_component.type + else if(component_type == /datum/component) + CRASH("[component_type] attempted instantiation!") + var/dupe_mode = initial(component_type.dupe_mode) var/dupe_type = initial(component_type.dupe_type) var/uses_sources = (dupe_mode == COMPONENT_DUPE_SOURCES) @@ -326,14 +337,6 @@ CRASH("Attempted to add a normal component of type '[component_type]' to '[type]' with a source!") var/datum/component/old_component - var/datum/component/new_component - - if(ispath(component_type)) - if(component_type == /datum/component) - CRASH("[component_type] attempted instantiation!") - else - new_component = component_type - component_type = new_component.type raw_args[1] = src if(dupe_mode != COMPONENT_DUPE_ALLOWED && dupe_mode != COMPONENT_DUPE_SELECTIVE) diff --git a/code/datums/elements/_element.dm b/code/datums/elements/_element.dm index ee1d5a9af9a..250b04a3620 100644 --- a/code/datums/elements/_element.dm +++ b/code/datums/elements/_element.dm @@ -53,6 +53,8 @@ if(QDELING(src)) CRASH("We just tried to add an element to a qdeleted datum, something is fucked") var/datum/element/ele = SSdcs.GetElement(arguments) + if(!ele) // We couldn't fetch the element, likely because it was not an element. + return // the crash message has already been sent arguments[1] = src if(ele.Attach(arglist(arguments)) == ELEMENT_INCOMPATIBLE) CRASH("Incompatible element [ele.type] was assigned to a [type]! args: [json_encode(args)]") @@ -63,6 +65,8 @@ */ /datum/proc/_RemoveElement(list/arguments) var/datum/element/ele = SSdcs.GetElement(arguments) + if(!ele) // We couldn't fetch the element, likely because it was not an element. + return // the crash message has already been sent if(ele.element_flags & ELEMENT_COMPLEX_DETACH) arguments[1] = src ele.Detach(arglist(arguments))