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 crashes when an iuse doesn't exist. #39849

Merged
merged 2 commits into from
Apr 25, 2020

Conversation

kevingranade
Copy link
Member

Summary

SUMMARY: None

Purpose of change

The recent removal of the ROYAL_JELLY iuse in #39782 triggered breakage in downstream mods that invoked it.
See https://discourse.cataclysmdda.org/t/segmentation-fault-on-finalizing-items/23367
The root cause is that the code handling iuse function lookup and assignment to itypes was generating a null "use_function" object, but was then not checking it for null before assigning it to the iuse, and it would inevitably be invoked during finalization, causing a crash.

Describe the solution

Adds a wrapper around iuse function lookup and assignment that refrains from emplacing the use_function if it is null. The lookup function already reports the failed lookup, so no further diagnostics are needed.

Describe alternatives you've considered

I considered trying to make the returned null object capable of being dereferenced, but that seems like a bandaid type fix that might be fragile in the future, and refraining from emplacing the function object is more robust.

Testing

Create an item that specifies a non-existent iuse id and load it.
It should display a debug message but continue to load.

@ZhilkinSerg ZhilkinSerg added <Bugfix> This is a fix for a bug (or closes open issue) [C++] Changes (can be) made in C++. Previously named `Code` labels Apr 24, 2020
src/item_factory.cpp Outdated Show resolved Hide resolved
src/item_factory.h Outdated Show resolved Hide resolved
@ZhilkinSerg
Copy link
Contributor

Legacy use actions (e.g. "use_action": "RPGDIE") with invalid data are reported and loading of game continues after that:

 DEBUG    : Received unrecognized iuse function RPGDIEZZZs, using iuse::none instead

 FUNCTION : use_function Item_factory::usage_from_string(const string&) const
 FILE     : src/item_factory.cpp
 LINE     : 2888

But modern use actions (e.g. "use_action": { "type": "bandolier", "capacity": 20, "ammo": [ "arrow", "bolt" ], "draw_cost": 20 }) with invalid data are reported and loading of game fails:

 DEBUG    : Received unrecognized iuse function bandolierXXX, using iuse::none instead

 FUNCTION : use_function Item_factory::usage_from_string(const string&) const
 FILE     : src/item_factory.cpp
 LINE     : 2888
 DEBUG    : Error: data/mods//dda/../../json/items/armor/ammo_pouch.json: line 259:28: unknown use_action

    "encumbrance": 3,
    "material_thickness": 1,
    "use_action": { "type":
                           ^
                            "bandolierXXX", "capacity": 20, "ammo": [ "arrow", "bolt" ], "draw_cost": 20 },
    "flags": [ "WAIST", "OVERSIZE", "WATER_FRIENDLY" ]
  },


 FUNCTION : bool main_menu::load_character_tab(bool)
 FILE     : src/main_menu.cpp
 LINE     : 1103

@kevingranade
Copy link
Member Author

I'll look at those too, I thought they were handled better.

@kevingranade kevingranade force-pushed the kevingranade-fix-missing-iuse-crash branch from f509e65 to f17268a Compare April 24, 2020 07:51
@kevingranade
Copy link
Member Author

So there's a tradeoff, if usage_from_object() doesn't throw an exception in this situation, we don't get as good of an error message when it fails, but I prefer the robustness for this situation.

@jbytheway
Copy link
Contributor

So there's a tradeoff, if usage_from_object() doesn't throw an exception in this situation, we don't get as good of an error message when it fails, but I prefer the robustness for this situation.

Is it worth throwing an error in test_mode?

@kevingranade
Copy link
Member Author

Is it worth throwing an error in test_mode?

Fantastic idea.

@kevingranade kevingranade force-pushed the kevingranade-fix-missing-iuse-crash branch from f17268a to cd5cdd1 Compare April 25, 2020 07:07
@ZhilkinSerg ZhilkinSerg merged commit 6bf174b into master Apr 25, 2020
@kevingranade kevingranade deleted the kevingranade-fix-missing-iuse-crash branch May 21, 2020 05:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
<Bugfix> This is a fix for a bug (or closes open issue) [C++] Changes (can be) made in C++. Previously named `Code`
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants