HeightMap: add GRID_MAX limit and input validation

Fixes potential integer overflow and invalid input issues:

- Add GRID_MAX constant (8192) to Common.h for global use
- Validate HeightMap dimensions against GRID_MAX to prevent
  integer overflow in w*h calculations (65536*65536 = 0)
- Add min > max validation for clamp() and normalize()
- Add unit tests for all new validation cases

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
John McCardle 2026-01-11 20:26:04 -05:00
commit 87444c2fd0
3 changed files with 90 additions and 0 deletions

View file

@ -102,6 +102,13 @@ int PyHeightMap::init(PyHeightMapObject* self, PyObject* args, PyObject* kwds)
return -1;
}
if (width > GRID_MAX || height > GRID_MAX) {
PyErr_Format(PyExc_ValueError,
"HeightMap dimensions cannot exceed %d (got %dx%d)",
GRID_MAX, width, height);
return -1;
}
// Clean up any existing heightmap
if (self->heightmap) {
TCOD_heightmap_delete(self->heightmap);
@ -253,6 +260,11 @@ PyObject* PyHeightMap::clamp(PyHeightMapObject* self, PyObject* args, PyObject*
return nullptr;
}
if (min_val > max_val) {
PyErr_SetString(PyExc_ValueError, "min must be less than or equal to max");
return nullptr;
}
TCOD_heightmap_clamp(self->heightmap, min_val, max_val);
// Return self for chaining
@ -277,6 +289,11 @@ PyObject* PyHeightMap::normalize(PyHeightMapObject* self, PyObject* args, PyObje
return nullptr;
}
if (min_val > max_val) {
PyErr_SetString(PyExc_ValueError, "min must be less than or equal to max");
return nullptr;
}
TCOD_heightmap_normalize(self->heightmap, min_val, max_val);
// Return self for chaining