Unsound slice::from_raw_parts() in git2::MergeFileResult::content()
When using `MergeResult::content()` when there is no content to retrieve, the method calls `slice::from_raw_parts()` in an unsound manner:
```rust
fn main() {
// Do something to trigger calls to git2::init(), otherwise the use of
// libgit2 stuff fails, e.g. the unwrapping below reports
// `called `Result::unwrap()` on an `Err` value: Error { code: -1, klass: 0, message: "no error" }`
let path = std::path::Path::new("");
let _index = git2::Index::open(&path).unwrap();
let base = git2::MergeFileInput::new();
let ours = git2::MergeFileInput::new();
let theirs = git2::MergeFileInput::new();
let result = git2::merge_file(&base, &ours, &theirs, None);
let result = result.unwrap();
println!("Content: {:#?}", result.content());
}
```
```
root@f3e233fd34d8:/rust/tests# cargo run
Compiling libc v0.2.186
Compiling bitflags v2.13.0
Compiling jobserver v0.1.34
Compiling cc v1.2.64
Compiling libz-sys v1.1.29
Compiling libgit2-sys v0.18.5+1.9.4 (https://github.com/rust-lang/git2-rs.git?branch=main#9a08ba3c)
Compiling git2 v0.21.0 (https://github.com/rust-lang/git2-rs.git?branch=main#9a08ba3c)
Compiling unsound-demo v0.1.0 (/rust/tests)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 30.27s
Running `target/debug/demo`
thread 'main' (1072) panicked at /usr/local/cargo/git/checkouts/git2-rs-f9f1e8cfc50c5ba7/9a08ba3/src/merge.rs:382: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.
Aborted (core dumped)
```
I reported this to the security team, and they said it is fine to report and fix publicly
关闭于 2026-06-20 0 条评论