`stash_foreach` crashes on stash entries with no message or a non-UTF-8 message

Aperta Adatta ai principianti
#1,320 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@shivampwm2020 ci sta già lavorando.

Dal 16/10/2026.

  • #1321 di @shivampwm2020 — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
78/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
rust
Ambito
api, backend

Direzione di ricerca

Inizia con stash_cb in src/stash.rs, dove l’issue individua la conversion del messaggio di stash in una stringa. Verifica come la callback gestisce un messaggio NULL e UTF-8 non valido, usando i passaggi di riproduzione segnalati per entrambi i casi. Il lavoro è concluso quando stash_foreach termina senza arrestarsi in modo anomalo né generare un panic e passa una stringa vuota se il messaggio manca.

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

Descrizione

Calling Repository::stash_foreach with git2 0.21.0 crashes when a stash reflog entry has no message. A non-UTF-8 reflog message causes a panic instead.

This affects applications that enumerate stashes when opening a repository, such as Gitte.

To reproduce this issue, create a repository with a stash, then remove the message from its reflog entry, eg like this:

mkdir stash-repro
cd stash-repro
git init
git config user.name "Test"
git config user.email "[email protected]"

printf 'base\n' > file.txt
git add file.txt
git commit -m "Initial commit"

printf 'changed\n' > file.txt
git stash push -m "Example stash"

cut -f1 .git/logs/refs/stash > .git/logs/refs/stash.tmp
mv .git/logs/refs/stash.tmp .git/logs/refs/stash

Then run this with git2 0.21.0:

fn main() -> Result<(), git2::Error> {
    let mut repo = git2::Repository::open(".")?;

    repo.stash_foreach(|index, message, oid| {
        println!("{index}: {oid} {message}");
        true
    })?;

    Ok(())
}

The process crashes with a segmentation fault. The enumeration should succeed and pass an empty string for the missing message.

The cause is this: in src/stash.rs, stash_cb converts the message with:

CStr::from_ptr(message).to_str().unwrap()

For a reflog entry without a message message is NULL. Passing it to CStr::from_ptr causes undefined behavior.

The same conversion also panics if a message contains invalid UTF-8. This can be reproduced by replacing the message part of the reflog entry with bytes such as b"caf\xe9".

As a workaround, I now use repo.reflog("refs/stash") directly and use entry.message_bytes() with String::from_utf8_lossy. This works with entries without a message and with entries with an invalid UTF-8 message.

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.