8000 Fix scaler memory safety, mouse clamp panic and Key transmute by emoon · Pull Request #431 · emoon/rust_minifb · GitHub
[go: up one dir, main page]

Skip to content

Fix scaler memory safety, mouse clamp panic and Key transmute - #431

Merged
emoon merged 1 commit into
masterfrom
fix/audit-memory-safety
Aug 29, 2026
Merged

emoon merged 1 commit into
masterfrom
fix/audit-memory-safety

Conversation

@emoon
@emoon emoon commented Aug 29, 2026
Copy link
Copy Markdown
Owner

Four audit findings, each reproduced locally before the fix and covered by a regression test.

Closes #406
Closes #407
Closes #422
Closes #424

Changes

Also fixes a build-script bug found while verifying: cc emits no cargo:rerun-if-changed, so cargo kept stale objects when only the C sources chan 8000 ged. Without it the scaler tests silently ran against the previous build.

Verification

cargo test on Linux/Wayland, plus an x11-only build. The four scaler and key tests fail against the pre-fix scalar.c and pass after.

Note: #355 (the reported AspectRatioStretch segfault) was already fixed by d62b0f5 and can be closed separately.

@emoon
emoon requested a balanced review from Copilot August 29, 2026 08:59
Copilot AI left a comment
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The scaler test guard is undersized for its cases, and one corrected overflow path remains untested.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Fixes memory-safety and correctness issues in shared input and POSIX scaling code.

Changes:

  • Hardens C scalers against overflow, invalid dimensions, and out-of-bounds writes.
  • Safely handles degenerate mouse dimensions and key conversions.
  • Adds rebuild directives and regression tests.
File summaries
File Description
build.rs Rebuilds changed native sources.
src/key.rs Fixes Key representation.
src/key_handler.rs Bounds key storage and conversions.
src/lib.rs Prevents mouse-clamp panic.
src/native/posix/scalar.c Fixes scaler arithmetic and bounds.
src/os/posix/common.rs Adds scaler regression tests.
Review details
  • Files reviewed: 6/6 changed files
  • Comments generated: 2
  • Review effort level: Balanced

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/os/posix/common.rs
Comment on lines +205 to +207
#[test]
fn resize_linear_handles_large_source_dimensions() {
let width = 2_200_000usize;
Comment thread src/os/posix/common.rs Outdated

let src: Vec<u32> = (0..src_width * src_height).map(|i| i as u32 | 1).collect();
let visible = dst_width * dst_height;
let mut dst = vec![GUARD_VALUE; visible + GUARD];
image_upper_left looped over src_height - (src_height - dst_height) / 2
rows into a dst_height buffer, writing past its end. Reachable from safe
code via ScaleMode::UpperLeft. Rewritten as a min(src, dst) row/column
copy, which is what upper-left anchoring means anyway.

Fixed-point coordinates in image_resize_linear and its stride variant
accumulated in a 32-bit int, so a source dimension above 2^21 wrapped
negative and read out of bounds. A 2.2M x 1 buffer is 8.8 MB and passes
check_buffer_size. Now int64_t, with each coordinate clamped to the
source extent.

The aspect-fill offsets were computed as (a - b) / -2 in unsigned
arithmetic, where -2 converts to 0xfffffffe, so the offset was always 0
or 1 and the image rendered top-left aligned instead of centered.

f32::clamp asserts min <= max, so get_mouse_pos in Clamp mode aborted
when the scale-divided window size fell below one pixel.

Key had no repr, leaving the discriminant layout unspecified while
key_handler transmuted u8 into it over a 512-entry range with a wrapping
cast. Key is now repr(u8), the transmutes go through a bounds-checked
key_from_index, and the state arrays are sized to the enum.

cc emits no cargo:rerun-if-changed for the files it compiles, so cargo
kept stale objects when only the native sources changed. Without this
the scaler tests silently ran against the previous build.

Closes #406
Closes #407
Closes #422
Closes #424
@emoon
emoon force-pushed the fix/audit-memory-safety branch from 033e6ef to bd0a4f1 Compare August 29, 2026 10:35
@emoon
emoon requested a balanced review from Copilot August 29, 2026 10:36
Copilot AI left a comment
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The audited failure modes are addressed with focused fixes and corresponding regression coverage.

Review details
  • Files reviewed: 6/6 changed files
  • Comments generated: 0 new
  • Review effort level: Balanced

@emoon
emoon merged commit 3a711ad into master Aug 29, 2026
9 checks passed
@emoon
emoon deleted the fix/audit-memory-safety branch August 29, 2026 10:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants

0