Skip to content

Panic when using gix::config::File::remove_section_filter #1826

@emilazy

Description

@emilazy

Current behavior 😯

Trying to iteratively remove subsections with a filter matching the metadata against the loaded repository configuration file’s metadata seems to panic.

Expected behavior 🤔

When running the provided test program, the [remote "origin"] sections that come from the loaded configuration file should be removed.

Git behavior

Presumably git config handles this properly :)

Steps to reproduce 🕹

yuyuko:~/Developer/gix-remote-bug  (no description set)
❭ cat src/main.rs
use std::{fs::File, sync::Arc};

use gix::bstr::BStr;
use tempfile::TempDir;

type Error = Box<dyn std::error::Error>;

fn set_up_repository(repo_dir: &TempDir) -> Result<(), Error> {
    let repo = gix::init(repo_dir)?;
    let mut config = repo.config_snapshot().clone();
    let config_meta = Arc::unwrap_or_clone(config.meta_owned());
    repo.remote_at("https://example.com/")?
        .save_as_to("origin", &mut config)?;
    let mut config_file = File::create(config_meta.path.clone().unwrap())?;
    config.write_to_filter(&mut config_file, |section| section.meta() == &config_meta)?;
    Ok(())
}

fn demonstrate_bug(repo_dir: TempDir) -> Result<(), Error> {
    let repo = gix::open(repo_dir.into_path())?;
    let mut config = repo.config_snapshot().clone();
    let config_meta = Arc::unwrap_or_clone(config.meta_owned());
    while config
        .remove_section_filter("remote", Some(BStr::new("origin")), |meta| {
            meta == &config_meta
        })
        .is_some()
    {}
    Ok(())
}

fn main() -> Result<(), Error> {
    let repo_dir = tempfile::tempdir()?;
    set_up_repository(&repo_dir)?;
    demonstrate_bug(repo_dir)?;
    Ok(())
}

yuyuko:~/Developer/gix-remote-bug  (no description set)
❭ cargo run
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.51s
     Running `target/debug/gix-remote-bug`

thread 'main' panicked at /Users/emily/.cache/cargo/registry/src/index.crates.io-1949cf8c6b5b557f/gix-config-0.43.0/src/file/access/mutate.rs:308:53:
each id has a section
stack backtrace:
   0: rust_begin_unwind
             at /rustc/99768c80a1c094a5cfc3b25a04e7a99de7210eae/library/std/src/panicking.rs:695:5
   1: core::panicking::panic_fmt
             at /rustc/99768c80a1c094a5cfc3b25a04e7a99de7210eae/library/core/src/panicking.rs:75:14
   2: core::panicking::panic_display
             at /rustc/99768c80a1c094a5cfc3b25a04e7a99de7210eae/library/core/src/panicking.rs:261:5
   3: core::option::expect_failed
             at /rustc/99768c80a1c094a5cfc3b25a04e7a99de7210eae/library/core/src/option.rs:2024:5
   4: core::option::Option<T>::expect
             at /nix/store/3zf4srnn9sc5hm7jk4g13kkmrc2jf39n-rust-default-1.86.0-nightly-2025-01-24/lib/rustlib/src/rust/library/core/src/option.rs:933:21
   5: gix_config::file::access::mutate::<impl gix_config::types::File>::remove_section_filter_inner::{{closure}}
             at /Users/emily/.cache/cargo/registry/src/index.crates.io-1949cf8c6b5b557f/gix-config-0.43.0/src/file/access/mutate.rs:308:31
   6: core::iter::traits::double_ended::DoubleEndedIterator::rfind::check::{{closure}}
             at /nix/store/3zf4srnn9sc5hm7jk4g13kkmrc2jf39n-rust-default-1.86.0-nightly-2025-01-24/lib/rustlib/src/rust/library/core/src/iter/traits/double_ended.rs:364:20
   7: core::iter::adapters::copied::copy_try_fold::{{closure}}
             at /nix/store/3zf4srnn9sc5hm7jk4g13kkmrc2jf39n-rust-default-1.86.0-nightly-2025-01-24/lib/rustlib/src/rust/library/core/src/iter/adapters/copied.rs:34:22
   8: core::iter::traits::double_ended::DoubleEndedIterator::try_rfold
             at /nix/store/3zf4srnn9sc5hm7jk4g13kkmrc2jf39n-rust-default-1.86.0-nightly-2025-01-24/lib/rustlib/src/rust/library/core/src/iter/traits/double_ended.rs:238:21
   9: <core::iter::adapters::copied::Copied<I> as core::iter::traits::double_ended::DoubleEndedIterator>::try_rfold
             at /nix/store/3zf4srnn9sc5hm7jk4g13kkmrc2jf39n-rust-default-1.86.0-nightly-2025-01-24/lib/rustlib/src/rust/library/core/src/iter/adapters/copied.rs:121:9
  10: core::iter::traits::double_ended::DoubleEndedIterator::rfind
             at /nix/store/3zf4srnn9sc5hm7jk4g13kkmrc2jf39n-rust-default-1.86.0-nightly-2025-01-24/lib/rustlib/src/rust/library/core/src/iter/traits/double_ended.rs:368:9
  11: <core::iter::adapters::rev::Rev<I> as core::iter::traits::iterator::Iterator>::find
             at /nix/store/3zf4srnn9sc5hm7jk4g13kkmrc2jf39n-rust-default-1.86.0-nightly-2025-01-24/lib/rustlib/src/rust/library/core/src/iter/adapters/rev.rs:72:9
  12: gix_config::file::access::mutate::<impl gix_config::types::File>::remove_section_filter_inner
             at /Users/emily/.cache/cargo/registry/src/index.crates.io-1949cf8c6b5b557f/gix-config-0.43.0/src/file/access/mutate.rs:304:18
  13: gix_config::file::access::mutate::<impl gix_config::types::File>::remove_section_filter
             at /Users/emily/.cache/cargo/registry/src/index.crates.io-1949cf8c6b5b557f/gix-config-0.43.0/src/file/access/mutate.rs:295:9
  14: gix_remote_bug::demonstrate_bug
             at ./src/main.rs:23:11
  15: gix_remote_bug::main
             at ./src/main.rs:35:5
  16: core::ops::function::FnOnce::call_once
             at /nix/store/3zf4srnn9sc5hm7jk4g13kkmrc2jf39n-rust-default-1.86.0-nightly-2025-01-24/lib/rustlib/src/rust/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions