From 2e5bf2ffb5f8489ce7798eeda4d3d7bd77d82d35 Mon Sep 17 00:00:00 2001 From: Tomas Scheel Date: Tue, 17 Dec 2024 14:01:07 -0800 Subject: [PATCH] Fixes SegFault w/ flashlights & attached power There was a segfault occuring on https://github.com/CleverRaven/Cataclysm-DDA/blob/master/src/item.cpp#L12277 when turning on a heavy-duty flashlight while it is simultaneously connected to a power source (ie. a vehicle) and does not have a battery. Added: - a short circuit check for ammo to address this issue in the future in case there are any other edge cases where this occurs. - a check for a linked power source so the light source uses the power source's linked power when determining the illumination value. Fixes #78580 Note: I used https://github.com/CleverRaven/Cataclysm-DDA/blob/master/src/item.cpp#L14177 as a reference for the linked power check above. This means the two power checks are extremely similar and it makes me believe that it would be beneficial to have a function such as `has_link_power` that returns a boolean (true for has power, false otherwise). My only hesitation is that I do not know where else that sort of power check is done and what the complete scope of the checks against `charge_rate` / `charge_interval` are for. --- src/item.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/item.cpp b/src/item.cpp index e00c94e4cb4ac..d648e91139584 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -12273,6 +12273,18 @@ int item::getlight_emit() const return 0; } if( has_flag( flag_CHARGEDIM ) && is_tool() && !has_flag( flag_USE_UPS ) ) { + // check if there's a link and it has a charge rate or charge interval + // if so, use its power supply + if (has_link_data() && ( link().charge_rate > 0 || link().charge_interval > 0 ) ) { + return lumint; + } + + // If we somehow got this far without a battery/power, + // return an illumination value of 0 + if( !has_ammo() ) { + return 0; + } + // Falloff starts at 1/5 total charge and scales linearly from there to 0. const ammotype &loaded_ammo = ammo_data()->ammo->type; if( ammo_capacity( loaded_ammo ) &&