Prevent integer overflows in bionic power #34684
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: Bugfixes "Fix integer overflows when bionic power is modified and when a value is converted to units::energy"
Purpose of change
Resolves #34665
The value by which power is modified in
Character::mod_power_level
could overflow when the new power was large enough that when it was added to the player's power_level the value it produced was larger than the value an int could store.An overflow could also happen in
units::from_(kilo)joule
when the value provided, when converted to millijoules, was larger than it's type could store.Describe the solution
Add checks to prevent overflow in all the functions mentioned.
Describe alternatives you've considered
Add a check in
units::from_millijoule
to prevent overflow, but I couldn't think of a good way to do that without loosing precision.Testing
Save that I used for testing provided.
overflow.tar.gz
To test for the overflow in
Character::mod_power_level
, eat the battery with ~650 charges (the character has 1980kJ of power, and their max capacity is the max power). The power will overflow and be clamped to 0kJ.To test for overflow in
units::from_kilojoule
, consume one of the batteries with 2500 charges, because this is more than can be stored in an int as a millijoule.