rust-lang / rust-lang/git2-rs

Segfault in `Index::add_all()` with empty `pathspecs` slice and `Some(cb)`

Open
#585 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
2.1k
Forks
450
Avg merge
11m
Merged PRs (30d)
1

Description

If Index::add_all() is called with an empty (&[]) pathspecs slice and Some(...) callback then it will crash.

If pathspecs is set to anything (eg &[*]) or cb is set to None then it runs without a crash.
I suspect the issue is that it's crashing trying to create the matched_pathspec argument for the callback and that the type of that argument should be changed to be an Option<&[u8]>.

Full steps to reproduce:

$ cargo new git2-crash
$ cd git2-crash
$ echo 'git2 = "0.13.6"' >> Cargo.toml
$ cat >|src/main.rs <<EOF
fn main() {
    let pathspecs: &[&str] = &[];
    git2::Repository::open(".")
        .unwrap()
        .index()
        .unwrap()
        .add_all(pathspecs, Default::default(), Some(&mut |_path, _matched_pathspec| 0))
        .unwrap();
}
EOF
$ cargo run
    Updating crates.io index
...
   Compiling git2-crash v0.1.0 (/Users/cpick/src/delme/git2-crash)
    Finished dev [unoptimized + debuginfo] target(s) in 14.64s
     Running `target/debug/git2-crash`
Segmentation fault: 11

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the crash with the provided cargo project and empty pathspecs slice, then inspect Index::add_all and its callback handling. The fix is complete when the example no longer segfaults with Some(callback), while the existing behavior for nonempty pathspecs and None remains intact; add a regression test if the repository's test structure supports it.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.