Fix: artificially limit heightmap sizes to something reasonable #9307
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
LGTM:
if ((uint64)width * height >= (size_t)-1) {
Comparison is always false because ... * ... <= 18446744065119617024.
Considering 18446744065119617023 a valid size is well outside of the realm of sanity for any of our players computers. So, I added some more sane limits.
If someone pushes a PNG or BMP with just under size_t dimensions into BaNaNaS and anyone trying to load that heightmap, it will crash their game when trying to allocate memory. After all, make the size so you want to allocate slightly less than (size_t)-1 and that allocation will fail as there is guaranteed not enough memory, as OpenTTD will already be using more than 1 byte.
Description
Introduce a limit on the maximum side length of an heightmap image to be at most 2 times
MAX_MAP_SIZE
(so currently 8192), and a limit on the maximum amount of bytes to allocated for a heightmap set at 1 gigabyte.With a 24bpp BMP that 1 GB allocation limit would result in an image with ~256 million pixels, so that limit won't be practically reached yet with 8192 maximum side lengths. However, when the maximum map size is changed, or at least the maximum size along one edge (such as in JGRPP) the size length limit becomes larger and this gets into play.
Limitations
See description; you can't load gigapixel size height maps anymore.
Checklist for review
Some things are not automated, and forgotten often. This list is a reminder for the reviewers.