# Document the invariants of the `unsafe` code in `Repository` (`src/repo.rs`)

Aperta
#1,339 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
35/100
Tipo di issue
Documentazione
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
rust
Ambito
documentation

Direzione di ricerca

Start with the unsafe blocks in src/repo.rs from the constructors through branch_from_annotated_commit, plus unsafe impl Send for Repository, Drop, RepositoryInitOptions::raw and Binding::from_raw. Read the libgit2 ownership rules for each call before writing anything. Done means each block in that range has a // SAFETY: comment stating its invariant, and clippy::undocumented_unsafe_blocks passes for that section. The issue's three lifetime and thread-safety concerns are separate issues and should not be fixed here.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

While auditing the first part of src/repo.rs at db31ed4 (constructors, open*/init*, getters
and setters through branch_from_annotated_commit, the foreach trampolines, Drop, Send,
RepositoryInitOptions), we found that none of its 85 unsafe blocks, nor unsafe impl Send for Repository, states the invariant it relies on. Checking them by hand, most hold, but several
implicit invariants do not:

  • C strings returned as &Path/&str are assumed to live as long as &self. That fails for
    workdir() (freed by set_workdir(&self)), namespace() (freed by set_namespace(&self)) and
    get_attr() (freed by the next attribute lookup). path() and commondir() are fine.
  • Send for Repository assumes nothing left on the old thread shares state with the repository.
    index() and config() return lifetime-free handles to the repository's own cached objects, so
    that does not hold.
  • set_refdb/set_config assume the installed object does not point back at another repository.
    For refdbs and repository configs it does.

Each of these is a separate issue with an AddressSanitizer or ThreadSanitizer trace.

Two smaller points on the public unsafe surface:

  • <Repository as Binding>::from_raw takes ownership (Drop calls git_repository_free), but
    Binding::from_raw's # Safety only says the pointer "must be valid".
  • RepositoryInitOptions::raw is unsafe, but calling it has no precondition. Only dereferencing
    the pointers it returns does, and that is already unsafe. It could be a safe fn.

A // SAFETY: comment per block would make these invariants reviewable, and
clippy::undocumented_unsafe_blocks can enforce it. The full per-site table is available on
request.

Found by Crustify, an experimental
UB/soundness auditing agent developed at UC Berkeley and running on Claude Opus 5.5, then
manually reviewed and independently reproduced.

Lingua principale
Rust
Stelle
2.1k
Fork
470
Merge medio
2g 17h
PR unite (30g)
18

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di rust-lang/git2-rs

Tutte le issue di rust-lang/git2-rs

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.