Fix: ensure no more than the allowed number of NewGRFs are loaded from the configuration #9430
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.
Motivation / Problem
When loading OpenTTD with a configuration that has more than NETWORK_MAX_GRF_COUNT NewGRFs configured bad things might happen.
The UI limits the user from entering more than that amount, but loading the configuration does not perform such sanity checks.
With older versions, including 1.11.2, this can cause an assertion or when assertions are disabled a buffer overrun when advertising to the master server.
With versions since the packet rewrite this causes only the assertion as the buffer will automatically resize if needed. However, the packets might be interpreted incorrectly by others, especially the UDP packets.
With any, but more likely for #9428, this could lead to an overflow of the number of NewGRFs in the packet, meaning the clients see 0 NewGRFs when the server actually has 256.
Description
The issue is solved by limiting the number of non-static NewGRFs that are loaded from the configuration file.
Limitations
Saving the configuration will now remove any NewGRFs over the limit from the configuration, so going back and forth between stable and post #9428 might loose NewGRFs in the configuration if you have set more than 62.
This change doesn't magically fix the problem of loading valid post #9428 settings files in 1.11. That requires this PR to be backported.
Checklist for review
Some things are not automated, and forgotten often. This list is a reminder for the reviewers.