Add input validation and fix Entity repr

- #212: Add GRID_MAX (8192) validation to Grid, ColorLayer, TileLayer
- #213: Validate color components are in 0-255 range
- #214: Add null pointer checks before HeightMap operations
- #216: Change entities_in_radius(x, y, radius) to (pos, radius)
- #217: Fix Entity __repr__ to show actual draw_pos float values

Closes #212, closes #213, closes #214, closes #216, closes #217

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
John McCardle 2026-01-16 19:18:27 -05:00
commit baa7ee354b
4 changed files with 228 additions and 13 deletions

View file

@ -118,6 +118,14 @@ static bool ParseColorArg(PyObject* obj, sf::Color& out_color, const char* arg_n
if (PyErr_Occurred()) return false;
// #213 - Validate color component range (0-255)
if (r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255 || a < 0 || a > 255) {
PyErr_Format(PyExc_ValueError,
"%s color components must be in range 0-255, got (%d, %d, %d, %d)",
arg_name, r, g, b, a);
return false;
}
out_color = sf::Color(r, g, b, a);
return true;
}
@ -818,6 +826,14 @@ int PyGridLayerAPI::ColorLayer_init(PyColorLayerObject* self, PyObject* args, Py
grid_x = PyLong_AsLong(PyTuple_GetItem(grid_size_obj, 0));
grid_y = PyLong_AsLong(PyTuple_GetItem(grid_size_obj, 1));
if (PyErr_Occurred()) return -1;
// #212 - Validate against GRID_MAX
if (grid_x > GRID_MAX || grid_y > GRID_MAX) {
PyErr_Format(PyExc_ValueError,
"ColorLayer dimensions cannot exceed %d (got %dx%d)",
GRID_MAX, grid_x, grid_y);
return -1;
}
}
// Create the layer (will be attached to grid via add_layer)
@ -897,6 +913,14 @@ PyObject* PyGridLayerAPI::ColorLayer_set(PyColorLayerObject* self, PyObject* arg
Py_DECREF(color_type);
return NULL;
}
// #213 - Validate color component range
if (r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255 || a < 0 || a > 255) {
Py_DECREF(color_type);
PyErr_Format(PyExc_ValueError,
"color components must be in range 0-255, got (%d, %d, %d, %d)",
r, g, b, a);
return NULL;
}
color = sf::Color(r, g, b, a);
} else {
Py_DECREF(color_type);
@ -938,6 +962,14 @@ PyObject* PyGridLayerAPI::ColorLayer_fill(PyColorLayerObject* self, PyObject* ar
Py_DECREF(color_type);
return NULL;
}
// #213 - Validate color component range
if (r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255 || a < 0 || a > 255) {
Py_DECREF(color_type);
PyErr_Format(PyExc_ValueError,
"color components must be in range 0-255, got (%d, %d, %d, %d)",
r, g, b, a);
return NULL;
}
color = sf::Color(r, g, b, a);
} else {
Py_DECREF(color_type);
@ -1005,6 +1037,14 @@ PyObject* PyGridLayerAPI::ColorLayer_fill_rect(PyColorLayerObject* self, PyObjec
Py_DECREF(color_type);
return NULL;
}
// #213 - Validate color component range
if (r < 0 || r > 255 || g < 0 || g > 255 || b < 0 || b > 255 || a < 0 || a > 255) {
Py_DECREF(color_type);
PyErr_Format(PyExc_ValueError,
"color components must be in range 0-255, got (%d, %d, %d, %d)",
r, g, b, a);
return NULL;
}
color = sf::Color(r, g, b, a);
} else {
Py_DECREF(color_type);
@ -1253,6 +1293,12 @@ PyObject* PyGridLayerAPI::ColorLayer_apply_hmap_threshold(PyColorLayerObject* se
return NULL;
}
// #214 - Check for null heightmap pointer
if (!hmap->heightmap) {
PyErr_SetString(PyExc_RuntimeError, "HeightMap is not initialized");
return NULL;
}
if (!ValidateHeightMapSize(hmap, self->data->grid_x, self->data->grid_y)) {
return NULL;
}
@ -1313,6 +1359,12 @@ PyObject* PyGridLayerAPI::ColorLayer_apply_gradient(PyColorLayerObject* self, Py
return NULL;
}
// #214 - Check for null heightmap pointer
if (!hmap->heightmap) {
PyErr_SetString(PyExc_RuntimeError, "HeightMap is not initialized");
return NULL;
}
if (!ValidateHeightMapSize(hmap, self->data->grid_x, self->data->grid_y)) {
return NULL;
}
@ -1375,6 +1427,12 @@ PyObject* PyGridLayerAPI::ColorLayer_apply_ranges(PyColorLayerObject* self, PyOb
return NULL;
}
// #214 - Check for null heightmap pointer
if (!hmap->heightmap) {
PyErr_SetString(PyExc_RuntimeError, "HeightMap is not initialized");
return NULL;
}
if (!ValidateHeightMapSize(hmap, self->data->grid_x, self->data->grid_y)) {
return NULL;
}
@ -1666,6 +1724,14 @@ int PyGridLayerAPI::TileLayer_init(PyTileLayerObject* self, PyObject* args, PyOb
grid_x = PyLong_AsLong(PyTuple_GetItem(grid_size_obj, 0));
grid_y = PyLong_AsLong(PyTuple_GetItem(grid_size_obj, 1));
if (PyErr_Occurred()) return -1;
// #212 - Validate against GRID_MAX
if (grid_x > GRID_MAX || grid_y > GRID_MAX) {
PyErr_Format(PyExc_ValueError,
"TileLayer dimensions cannot exceed %d (got %dx%d)",
GRID_MAX, grid_x, grid_y);
return -1;
}
}
// Create the layer
@ -1801,6 +1867,12 @@ PyObject* PyGridLayerAPI::TileLayer_apply_threshold(PyTileLayerObject* self, PyO
return NULL;
}
// #214 - Check for null heightmap pointer
if (!hmap->heightmap) {
PyErr_SetString(PyExc_RuntimeError, "HeightMap is not initialized");
return NULL;
}
if (!ValidateHeightMapSize(hmap, self->data->grid_x, self->data->grid_y)) {
return NULL;
}
@ -1851,6 +1923,12 @@ PyObject* PyGridLayerAPI::TileLayer_apply_ranges(PyTileLayerObject* self, PyObje
return NULL;
}
// #214 - Check for null heightmap pointer
if (!hmap->heightmap) {
PyErr_SetString(PyExc_RuntimeError, "HeightMap is not initialized");
return NULL;
}
if (!ValidateHeightMapSize(hmap, self->data->grid_x, self->data->grid_y)) {
return NULL;
}