Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes SegFault w/ flashlights & attached power #78633

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

TRScheel
Copy link
Contributor

Summary

Bugfixes "Fixes SegFault w/ flashlights & attached power"

Purpose of change

There was a segfault occuring on

const ammotype &loaded_ammo = ammo_data()->ammo->type;
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. See #78580

Describe the solution

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.

Describe alternatives you've considered

I didn't consider any other alternatives.

Testing

I loaded CDDA in a dev environment prior to this fix and created a test world and saved it once I had the conditions necessary to reproduce the bug. In that test world, I then turned on the flashlight and successfully reproduced the bug.

After applying the fix, I loaded the same save and attempted the same action. This time successfully due to the fix. In addition, I spawned a normal vehicle, attached the flashlight to it, and ensured there was a power draw occurring (there was). Then I removed the battery while the flashlight was on to ensure that it both turned off (it did) and that the game did not crash (it did not).

Additional context

I used

if( !has_link_data() || link().charge_rate == 0 || link().charge_interval < 1 ) {
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.

@github-actions github-actions bot added [C++] Changes (can be) made in C++. Previously named `Code` <Bugfix> This is a fix for a bug (or closes open issue) json-styled JSON lint passed, label assigned by github actions BasicBuildPassed This PR builds correctly, label assigned by github actions astyled astyled PR, label is assigned by github actions labels Dec 17, 2024
@TRScheel TRScheel force-pushed the bugfix/78580/flashlight-segfault branch from cd9826e to ceddbd3 Compare December 18, 2024 02:32
@github-actions github-actions bot removed the astyled astyled PR, label is assigned by github actions label Dec 18, 2024
@TRScheel TRScheel force-pushed the bugfix/78580/flashlight-segfault branch from ceddbd3 to 2e5bf2f Compare December 18, 2024 02:34
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.
@TRScheel TRScheel force-pushed the bugfix/78580/flashlight-segfault branch from 2e5bf2f to ed7a0e6 Compare December 18, 2024 02:36
@github-actions github-actions bot added the astyled astyled PR, label is assigned by github actions label Dec 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
astyled astyled PR, label is assigned by github actions BasicBuildPassed This PR builds correctly, label assigned by github actions <Bugfix> This is a fix for a bug (or closes open issue) [C++] Changes (can be) made in C++. Previously named `Code` json-styled JSON lint passed, label assigned by github actions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant