Fix critical bugs in FakeAircraftClass: division by zero, null derefs, and unreachable code #9
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.
Addresses five critical bugs and improves code maintainability in aircraft mission logic.
Critical Bugs Fixed
Division by zero in velocity calculations (lines 629, 728)
Unreachable code in facing calculation (lines 121-132)
elseblock caused unconditional overwrite offac.Raw = 0Operator precedence in strafe calculation (line 446)
Null pointer dereferences
_GetWeapon():pExtnot validated before dereference_FireAt():pTargetandpTypeExtnot validated_GreatestThreat(): weapon struct pointers not validatedCode Quality Improvements
AircraftConstantsnamespaceSelectWeapon()results eliminating 20+ redundant calls in mission loopsFiles Changed
src/Ext/Aircraft/Body.h- Constants namespacesrc/Ext/Aircraft/Body.cpp- Bug fixes and optimizationssrc/Ext/BuildingType/Hooks.DockPoseDir.cpp- Magic number replacementOriginal prompt
Overview
This PR addresses several bugs identified in the
FakeAircraftClassimplementation and related aircraft docking/factory code, along with code quality improvements.Files to Modify
src/Ext/Aircraft/Body.cppsrc/Ext/Aircraft/Body.hsrc/Ext/BuildingType/Hooks.DockPoseDir.cppBug Fixes Required
Bug 1: Unreachable Code in
_Mission_Attack()(Body.cpp, Lines 121-132)The code sets
fac.Raw = 0when coordinates match but then continues to overwrite it immediately. Theelsebranch is missing.Current code:
Fix: Add
elseblock so the calculation only runs when coordinates don't match:Bug 2: Division by Zero Risk in
CalculateVelocity()(Body.cpp, Line 629)The calculation
aircraftSpeed / distcan cause division by zero or floating-point issues with very small values.Current code:
Fix: Add early return for zero/near-zero distance:
Also apply similar fix around line 727-728:
Bug 3: Operator Precedence Error in
_Mission_Attack()Strafe (Body.cpp, Line 446)The return value calculation has incorrect operator precedence.
Current code:
Fix: Add parentheses for correct calculation:
Bug 4: Potential Null Pointer in
_GetWeapon()(Body.cpp, Lines 833-841)The
pExtpointer is not checked for null before dereferencing.Current code:
Fix: Add null check:
Bug 5: Missing Null Check in
_FireAt()(Body.cpp, Line 760)The function should validate that
pTargetis not null before proceeding.Fix: Add null check at the start of
_FireAt():Code Quality Improvements
1. Add Named Constants (Body.cpp or Body.h)
Add a namespace with named constants to replace magic numbers:
Then replace the magic numbers throughout the code:
512→AircraftConstants::MinDistanceForFacing16→AircraftConstants::MinDistanceToTarget45→AircraftConstants::FacingErrorTimeout768→AircraftConstants::LandingProximity1024→AircraftConstants::StrafeCalculationOffset0.001→AircraftConstants::MinVelocityLength2. Add Null Checks in
_GreatestThreat()(Body.cpp, Lines 503-515)The weapon pointers should be validated:
Current code:
Fix: