fix: segfault when siphoning a vehicle with multiple tanks #40227
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 "fixes segfault when attempting to siphon from vehicle with multiple tanks"
Purpose of change
When siphoning from a vehicle with more than one tank and the first tank is empty, during the check to auto-select the first tank with contents, a null pointer would be returned from
pt.base.contents.legacy_front()
ultimately resulting in a segfault.Describe the solution
Remove the
legacy_front()
call and replace it with a proper!empty()
check andonly_item()
call.Describe alternatives you've considered
N/A
Testing
First start a fresh game, give yourself a jerrycan and a hose (and then remove the hose from the jerrycan ...), go find a vehicle with 2 fuel tanks, siphon the first, and then siphon the second. You should see a segfault.
Apply patch, repeat, no more segfault.
Additional context
This
legacy_front()
thing is extremely unsafe and may be the cause of several other crashes I'm seeing during playtesting. I put a debugmsg in it when it's about to return a nullptr, because this is almost always going to result in undefined behavior and this should help narrow down when this was the proximate cause.