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

オープン 初心者向け
#1,320 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@shivampwm2020 がすでに取り組んでいます。

2026年10月16日 から。

  • #1321 @shivampwm2020 による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
78/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
rust
領域
api, backend

調査の方向性

src/stash.rsで、issueがstashメッセージの文字列への変換箇所として示しているstash_cbから始めてください。報告された両方のケースの再現手順を使い、callbackがNULLメッセージと無効なUTF-8をどのように処理するか確認してください。stash_foreachがクラッシュやpanicを起こさずに完了し、メッセージがない場合に空文字列を渡せば完了です。

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
Rust
スター
2.1k
フォーク
470
平均マージ
2日 17時間
マージ済み PR(30日)
18

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

rust-lang/git2-rs のほかの issue

rust-lang/git2-rs の issue をすべて見る

似ている issue

Rust の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。