Skip to content

Commit

Permalink
Fix image load NULL dereference and verify the existence of more files
Browse files Browse the repository at this point in the history
  • Loading branch information
HexDecimal committed Jul 16, 2024
1 parent f83948e commit 793b541
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ This project adheres to [Semantic Versioning](https://semver.org/) since version
### Fixed

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

## [16.2.2] - 2024-01-16

Expand Down
9 changes: 8 additions & 1 deletion tcod/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,16 @@ def __init__(self, width: int, height: int) -> None:
"""Initialize a blank image."""
self.width, self.height = width, height
self.image_c = ffi.gc(lib.TCOD_image_new(width, height), lib.TCOD_image_delete)
if self.image_c == ffi.NULL:
msg = "Failed to allocate image."
raise MemoryError(msg)

@classmethod
def _from_cdata(cls, cdata: Any) -> Image: # noqa: ANN401
self: Image = object.__new__(cls)
if cdata == ffi.NULL:
msg = "Pointer must not be NULL."
raise RuntimeError(msg)
self.image_c = cdata
self.width, self.height = self._get_size()
return self
Expand Down Expand Up @@ -365,7 +371,8 @@ def load(filename: str | PathLike[str]) -> NDArray[np.uint8]:
.. versionadded:: 11.4
"""
image = Image._from_cdata(ffi.gc(lib.TCOD_image_load(_path_encode(Path(filename))), lib.TCOD_image_delete))
filename = Path(filename).resolve(strict=True)
image = Image._from_cdata(ffi.gc(lib.TCOD_image_load(_path_encode(filename)), lib.TCOD_image_delete))
array: NDArray[np.uint8] = np.asarray(image, dtype=np.uint8)
height, width, depth = array.shape
if depth == 3: # noqa: PLR2004
Expand Down
7 changes: 4 additions & 3 deletions tcod/libtcodpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3434,7 +3434,7 @@ def mouse_get_status() -> Mouse:

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


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

propagate_manager = _PropagateException()
Expand All @@ -3656,7 +3657,7 @@ def parser_run(parser: Any, filename: str | PathLike[str], listener: Any = None)
with _parser_callback_lock:
_parser_listener = listener
with propagate_manager:
lib.TCOD_parser_run(parser, _path_encode(Path(filename)), c_listener)
lib.TCOD_parser_run(parser, _path_encode(filename), c_listener)


@deprecate("libtcod objects are deleted automatically.")
Expand Down

0 comments on commit 793b541

Please sign in to comment.