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

Prevent segfault when unloading from spillable container #75299

Conversation

inogenous
Copy link
Contributor

Summary

Bugfixes "Prevent segfault when unloading from spillable container"

Purpose of change

Prevents segfault from accessing an invalidated item_location when unloading from a container that itself is inside a spillable container. Fixes #75156 .

Describe the solution

The previous segfault could be reproduced by having an inventory like:

backpack >
  steel_pan >
    bottle_plastic_small > antacid tablet (1)
    bottle_plastic_small > antacid tablet (4)

And then unloading the single pill bottle caused a segfault. What happens is that part of unloading will trigger the steelpan to spill its contents. Part of spilling the contents makes the item_location invalidated, as previously documented in ::handle_contents_changed in character.h.

Cataclysm-DDA/src/character.h

Lines 2129 to 2130 in 67026ae

* single batch; items (not limited to the ones listed in `containers` and
* their contents) may be invalidated or moved after a call to this function.

This change instead makes sure that we do not use item_location or the item after we have called contents_change_handler::handle_by. From the example above, the steelpan will still spill its contents, but the segfault can no longer be reproduced. This change also adds a user-visible message when spilling non-liquid items to make it obvious to the player what happened (there was no message at all before).

Describe alternatives you've considered

Testing

Can no longer reproduce the segfault in #75156 with this change.

Additional context

It's odd that we were able to put the antacid-pill-bottles in the steelpan in the first place, since it's a spillable container, and that should not be possible in a backpack. But that's for a different issue.

@github-actions github-actions bot added <Bugfix> This is a fix for a bug (or closes open issue) Code: Tests Measurement, self-control, statistics, balancing. [C++] Changes (can be) made in C++. Previously named `Code` Items: Containers Things that hold other things labels Jul 28, 2024
@github-actions github-actions bot added astyled astyled PR, label is assigned by github actions json-styled JSON lint passed, label assigned by github actions labels Jul 28, 2024
Prevents segfault from accessing an invalidated `item_location` when
unloading from a container that itself is inside a spillable container.

The previous segfault could be reproduced by having an inventory like:
```
backpack >
  steel_pan >
    bottle_plastic_small > antacid tablet (1)
    bottle_plastic_small > antacid tablet (4)
```
And then unloading the single pill bottle caused a segfault. What
happens is that part of unloading will trigger the steelpan to spill
its contents. Part of spilling the contents makes the `item_location`
invalidated, as previously documented in `::handle_contents_changed` in
`character.h`.

This commit instead makes sure that we do not use `item_location` or the
item after we have called `contents_change_handler::handle_by`. From the
example above, the steelpan will still spill its contents, but the
segfault can no longer be reproduced. This commmit also adds a
user-visible message when spilling non-liquid items.
@inogenous inogenous force-pushed the prevent-segfault-unloading-spillable-container branch from 07f3682 to 8573bd7 Compare July 28, 2024 21:57
@github-actions github-actions bot added the BasicBuildPassed This PR builds correctly, label assigned by github actions label Jul 29, 2024
@Maleclypse Maleclypse merged commit 7382ea8 into CleverRaven:master Jul 29, 2024
26 checks passed
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` Code: Tests Measurement, self-control, statistics, balancing. Items: Containers Things that hold other things json-styled JSON lint passed, label assigned by github actions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Segfault on unloading bottle of antacid tablets
2 participants