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

Don't access items in empty cache #2332

Merged
merged 4 commits into from
Sep 1, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions common/city.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2247,6 +2247,9 @@ void add_specialist_output(
// This is more than just an optimization. For governors that forbid
// specialists, the cache may not be filled.
if (count > 0) {
// If there is a cache it must not be empty.
fc_assert(!pcsoutputs || !pcsoutputs->empty());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fc_ asserts aren't fatal by default. Use fc_assert_action(condition, continue) to skip this specialist, which is probably an ok fallback behavior:

Suggested change
fc_assert(!pcsoutputs || !pcsoutputs->empty());
fc_assert_action(!pcsoutputs || !pcsoutputs->empty(), continue);

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I implemented another solution, falling back to the old behaviour by setting the cache to nullptr if the assertion triggers. This makes sure, that the game will behave correctly. What do you think?


output_type_iterate(stat_index)
{
int amount = pcsoutputs
Expand Down Expand Up @@ -2876,6 +2879,9 @@ void set_city_production(struct city *pcity,
* on, so shield waste will include shield bonuses. */
output_type_iterate(o)
{
// If there is a cache it must not be empty.
fc_assert(!pcwaste || !pcwaste->empty());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably go before the loop to avoid getting 6 messages every time. I think returning if the assert fails is fine, so use fc_assert_ret(condition).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.


pcity->waste[o] =
city_waste(pcity, o, pcity->prod[o] * pcity->bonus[o] / 100, nullptr,
gov_centers, pcwaste ? &pcwaste->at(o) : nullptr);
Expand Down
Loading