Skip to content

Commit

Permalink
Fixes SegFault w/ flashlights & attached power
Browse files Browse the repository at this point in the history
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 CleverRaven#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.
  • Loading branch information
TRScheel committed Dec 17, 2024
1 parent 1be27d7 commit 800c9a4
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12273,6 +12273,19 @@ 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 ) &&
Expand Down

0 comments on commit 800c9a4

Please sign in to comment.