Prevent undefined behaviour modifying bionic power #36686
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
SUMMARY: None
Purpose of change
#34684 (comment)
I needed a way to instantly modify bionic power so that I could test that this fix worked.
Describe the solution
Add a
Debug Bionic Powergen
trait, which when activated prompts you for a number by which to modify bionic power.Cast
npower
andpower_level
toint64_t
, then add them and compare them with max power level to prevent integer overflow inCharacter::mod_bionic_power()
Describe alternatives you've considered
Leaving the UB there, like I did for the past 30 or so days.
Testing
Create a character, install enough bionic power storage to push your max power level to the max. Debug in the
Debug Bionic Powergen
trait, activate it and then increase your power by2000000000
using it. Repeat, and notice that power does not overflow (where it is clamped to 0), and instead your power stays at the max power level.If you did that without these changes and the changes in #34684 applied, it will overflow and be clamped to 0.