ITADN

Creating Pixmap with 0x0 dimensions results in UB in safe code

#175Closedwezm 创建于 2025-10-17
W
wezmcommented
Noticed via https://github.com/vincent-uden/miro/issues/52 Minimal reproducer: ```rust #!/usr/bin/env -S cargo +nightly -Zscript --- package.edition = "2024" [dependencies] mupdf = "0.5" --- use mupdf::pixmap::Pixmap; use mupdf::colorspace::Colorspace; fn main() { let width=0; let height=0; let pix = Pixmap::new_with_w_h(&Colorspace::device_rgb(), width, height, true).unwrap(); pix.samples(); } ``` Run with: `cargo +nightly -Zscript mupdf.rs` (nightly only need for for [cargo script](https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#script) support). ``` Compiling mupdf v0.0.0 (/home/wmoore/Projects/mupdf-repo/mupdf.rs) Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.13s Running `/home/wmoore/.cargo/build/40/b8f2caa1d0cf01/target/debug/mupdf` thread 'main' (2028451) panicked at /home/wmoore/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/mupdf-0.5.0/src/pixmap.rs:146:18: unsafe precondition(s) violated: slice::from_raw_parts requires the pointer to be aligned and non-null, and the total size of the slice not to exceed `isize::MAX` This indicates a bug in the program. This Undefined Behavior check is optional, and cannot be relied on for safety. note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace thread caused non-unwinding panic. aborting. [1] 2028451 IOT instruction (core dumped) cargo +nightly -Zscript mupdf.rs ```
关闭于 2025-10-17 1 条评论