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

Fix: artificially limit heightmap sizes to something reasonable #9307

Merged
merged 1 commit into from
May 30, 2021

Conversation

rubidium42
Copy link
Contributor

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.

  • The bug fix is important enough to be backported? (label: 'backport requested')
  • This PR affects the save game format? (label 'savegame upgrade')
  • This PR affects the GS/AI API? (label 'needs review: Script API')
    • ai_changelog.hpp, gs_changelog.hpp need updating.
    • The compatibility wrappers (compat_*.nut) need updating.
  • This PR affects the NewGRF API? (label 'needs review: NewGRF')

@rubidium42 rubidium42 changed the title Change: artificially limit heightmap sizes to something reasonable Fix: artificially limit heightmap sizes to something reasonable May 29, 2021
@rubidium42 rubidium42 added the backport requested This PR should be backport to current release (RC / stable) label May 29, 2021
@James103
Copy link
Contributor

See description; you can't load gigapixel size height maps anymore

A workaround is to use an external image processing software (such as GIMP or Paint.net) to scale/resize and crop the input height map to whatever playing area you want and at the size you want.

src/heightmap.cpp Outdated Show resolved Hide resolved
src/heightmap.cpp Outdated Show resolved Hide resolved
src/heightmap.cpp Outdated Show resolved Hide resolved
src/heightmap.cpp Outdated Show resolved Hide resolved
src/heightmap.cpp Show resolved Hide resolved
src/heightmap.cpp Outdated Show resolved Hide resolved
src/heightmap.cpp Show resolved Hide resolved
@rubidium42 rubidium42 merged commit 97c461d into OpenTTD:master May 30, 2021
@rubidium42 rubidium42 deleted the lgtm3 branch May 30, 2021 07:50
@TrueBrain TrueBrain removed the backport requested This PR should be backport to current release (RC / stable) label Oct 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants