Skip to content

Small safety fixes: RNC header bounds, hand-rule guards, multi-monitor frontend#4986

Closed
matthewdeaves wants to merge 3 commits into
dkfans:masterfrom
matthewdeaves:fixes-and-guards
Closed

Small safety fixes: RNC header bounds, hand-rule guards, multi-monitor frontend#4986
matthewdeaves wants to merge 3 commits into
dkfans:masterfrom
matthewdeaves:fixes-and-guards

Conversation

@matthewdeaves

Copy link
Copy Markdown

Three small, independent fixes — each in its own commit.

1. dernc: bounds-check UnpackM1
It copied the 18-byte RNC header out of the buffer before checking the buffer was that big, so a file smaller than the header caused an out-of-bounds read. Now it returns early (as "not compressed") when the buffer is too small.

2. power_hand: guard invalid dungeon and out-of-range model
thing_pickup_is_blocked_by_hand_rule() indexed dungeon->hand_rules[model] without checking the dungeon was valid or that model was in [0, CREATURE_TYPES_MAX). An invalid dungeon (e.g. an unowned player) or a bad model dereferenced a bad pointer / read out of bounds. Both are now guarded, with a log line for the offending thing.

3. video: pick the display under the cursor for the pre-window frontend
Before a game window exists (splash/frontend), LbGetCurrentDisplayIndex() always returned display 0, so on multi-monitor setups the splash could size for the wrong screen. It now picks the display containing the mouse cursor, falling back to the old default. (Also the monitor a Wayland compositor opens the window on.)

The first two are adapted from @cerwym's fork, the third from @DoubyCz's — credited in the commit messages. I came across them while working on a native macOS build and they looked worth sending on their own.

Disclosure: reviewed and prepared with the help of Claude Code.

matthewdeaves and others added 3 commits July 5, 2026 18:35
UnpackM1() memcpy'd sizeof(rnc_header) bytes out of the caller's buffer
before checking its size, an out-of-bounds read when the buffer is smaller
than the RNC header. Return 0 (the existing "not compressed" result) if the
buffer can't even hold a header.

Adapted from cerwym/keeperfx@643f618e (Peter Lockett).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
thing_pickup_is_blocked_by_hand_rule() indexed dungeon->hand_rules[model][i]
without validating either: an invalid dungeon (e.g. neutral/unowned player)
dereferenced a bad pointer, and a creature model outside [0, CREATURE_TYPES_MAX)
read out of bounds of the hand_rules array. Guard both and log the offending
thing.

Adapted from cerwym/keeperfx@16cdd064 (Peter Lockett).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
LbGetCurrentDisplayIndex() defaulted to display 0 when no game window
exists yet (splash/legal/frontend setup), so on a multi-monitor setup the
splash and frontend could size for the wrong monitor. When there's no
window, query the global cursor position and pick the display containing
it — this is also the monitor a Wayland compositor will open the window on.
Helps macOS/Linux/Windows multi-monitor alike.

Adapted from DoubyCz/keeperfx@3b5460a7b3.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@cerwym

cerwym commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

I guess I should be flattered, but please adjust the commit messages to have the original author as a Co-Author, much like Claude is appending;

So, instruct your tool to add 'Co authored by cerwym (1760289+cerwym@users.noreply.github.com);

This will correctly list me and other authors in the blame for git.

@yani

yani commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

Number 3 is completely wrong. Keep that LLM poop out of here. The context of other forks doesn't matter as long as it isn't part of the master. We set the correct monitor trough the config variable DISPLAY_SCREEN:

unsigned short current_display_id = display_id; // default to the already set display_id

This PR breaks that functionality and introduces the cursor placement position which nobody wants when starting up a game.

@matthewdeaves matthewdeaves deleted the fixes-and-guards branch July 7, 2026 16:03
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.

3 participants