Skip to content

Commit 793b541

Browse files
committed
Fix image load NULL dereference and verify the existence of more files
1 parent f83948e commit 793b541

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ This project adheres to [Semantic Versioning](https://semver.org/) since version
99
### Fixed
1010

1111
- Fixed access violation when events are polled before SDL is initialized.
12+
- Fixed access violation when libtcod images fail to load.
13+
- Verify input files exist when calling `libtcodpy.parser_run`, `libtcodpy.namegen_parse`, `tcod.image.load`.
1214

1315
## [16.2.2] - 2024-01-16
1416

tcod/image.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,16 @@ def __init__(self, width: int, height: int) -> None:
3939
"""Initialize a blank image."""
4040
self.width, self.height = width, height
4141
self.image_c = ffi.gc(lib.TCOD_image_new(width, height), lib.TCOD_image_delete)
42+
if self.image_c == ffi.NULL:
43+
msg = "Failed to allocate image."
44+
raise MemoryError(msg)
4245

4346
@classmethod
4447
def _from_cdata(cls, cdata: Any) -> Image: # noqa: ANN401
4548
self: Image = object.__new__(cls)
49+
if cdata == ffi.NULL:
50+
msg = "Pointer must not be NULL."
51+
raise RuntimeError(msg)
4652
self.image_c = cdata
4753
self.width, self.height = self._get_size()
4854
return self
@@ -365,7 +371,8 @@ def load(filename: str | PathLike[str]) -> NDArray[np.uint8]:
365371
366372
.. versionadded:: 11.4
367373
"""
368-
image = Image._from_cdata(ffi.gc(lib.TCOD_image_load(_path_encode(Path(filename))), lib.TCOD_image_delete))
374+
filename = Path(filename).resolve(strict=True)
375+
image = Image._from_cdata(ffi.gc(lib.TCOD_image_load(_path_encode(filename)), lib.TCOD_image_delete))
369376
array: NDArray[np.uint8] = np.asarray(image, dtype=np.uint8)
370377
height, width, depth = array.shape
371378
if depth == 3: # noqa: PLR2004

tcod/libtcodpy.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3434,7 +3434,7 @@ def mouse_get_status() -> Mouse:
34343434

34353435
@pending_deprecate()
34363436
def namegen_parse(filename: str | PathLike[str], random: tcod.random.Random | None = None) -> None:
3437-
lib.TCOD_namegen_parse(_path_encode(Path(filename)), random or ffi.NULL)
3437+
lib.TCOD_namegen_parse(_path_encode(Path(filename).resolve(strict=True)), random or ffi.NULL)
34383438

34393439

34403440
@pending_deprecate()
@@ -3636,8 +3636,9 @@ def _pycall_parser_error(msg: Any) -> None:
36363636
@deprecate("Parser functions have been deprecated.")
36373637
def parser_run(parser: Any, filename: str | PathLike[str], listener: Any = None) -> None:
36383638
global _parser_listener # noqa: PLW0603
3639+
filename = Path(filename).resolve(strict=True)
36393640
if not listener:
3640-
lib.TCOD_parser_run(parser, _path_encode(Path(filename)), ffi.NULL)
3641+
lib.TCOD_parser_run(parser, _path_encode(filename), ffi.NULL)
36413642
return
36423643

36433644
propagate_manager = _PropagateException()
@@ -3656,7 +3657,7 @@ def parser_run(parser: Any, filename: str | PathLike[str], listener: Any = None)
36563657
with _parser_callback_lock:
36573658
_parser_listener = listener
36583659
with propagate_manager:
3659-
lib.TCOD_parser_run(parser, _path_encode(Path(filename)), c_listener)
3660+
lib.TCOD_parser_run(parser, _path_encode(filename), c_listener)
36603661

36613662

36623663
@deprecate("libtcod objects are deleted automatically.")

0 commit comments

Comments
 (0)