Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

`Deque::make_contiguous` sets a wrong `back` for a wrapped, non-full deque: `len()` grows and elements are dropped twice

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

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

2026年10月26日 から。

  • #695 @sgued による — オープン

評価

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

調査の方向性

src/deque.rs の Deque::make_contiguous から始め、issue で説明されている、満杯ではなく折り返した分岐を追ってください。移動の前後で front、back、len() がどう関係するかを確認し、容量 8、front が 4、要素数が 5 の回帰ケースを実行するか追加してください。make_contiguous() の後も len() が 5 のままで、各要素が一度だけ破棄されれば完了です。

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

説明


First of all, thank you so much for maintaining heapless; while applying our testing tool across multiple crates during its evaluation, we encountered an issue that I would like to bring to your attention.

Deque::make_contiguous on a wrapped deque that is not full can set the back cursor to the wrong slot. After that, len() reports more elements than the deque holds, and safe code reads and drops slots that hold no value.

I notice the following code may have an unsound problem: https://github.com/rust-embedded/heapless/blob/1d3dace9d0ec77f8546da87b0b94873a07b98ad9/src/deque.rs#L404-L427

This is a different case from #676 / #677 (full deque). The branch below is reached by a deque that is not full.

Describe the bug

} else if free >= back_len {
    // from: FGH....ABCDE
    // to:   ...ABCDEFGH.
    unsafe {
        ptr::copy(buffer_ptr.add(self.front), buffer_ptr.add(self.back), front_len);
        ptr::copy_nonoverlapping(buffer_ptr, buffer_ptr.add(self.back + front_len), back_len);
    }

    self.front = back;
    self.back = 0;
}

After the copies the elements are in back..back + len, so back should be back + len. The code sets it to 0. Then len() returns capacity - front instead of len. This is right only when the elements happen to end at the last slot.

Example with capacity 8 and 5 elements in slots 4,5,6,7,0: the elements move to slots 1..6. front becomes 1 and back becomes 0, so len() returns 8 - 1 = 7. iter(), pop_*(), truncate(), clear() and Drop then treat 2 extra slots as elements. With Box elements this is a double free.

Tested on the latest release, heapless 0.9.3 (main 1d3dace has the same code).

To Reproduce

  1. Push and pop 4 times on a Deque<_, 8>, so that front is 4.
  2. Push 5 elements. They wrap into slots 4,5,6,7,0, and the deque is not full.
  3. Call make_contiguous().
use heapless::Deque;

fn main() {
    let mut dq: Deque<Box<u64>, 8> = Deque::new();

    // move `front` to slot 4
    for _ in 0..4 {
        dq.push_back(Box::new(0)).unwrap();
        dq.pop_front();
    }
    // 5 elements in slots 4,5,6,7,0: wrapped, not full
    for i in 0..5 {
        dq.push_back(Box::new(i)).unwrap();
    }

    println!("len before = {}", dq.len());
    dq.make_contiguous();
    println!("len after  = {}", dq.len());
} // drop: 7 slots are dropped, 2 of them twice

make_contiguous should not change len(): len after = 5, and each box should be freed once.

output (RUSTFLAGS="-Zsanitizer=address" cargo +nightly-2026-04-03 run --release --target x86_64-unknown-linux-gnu):

len before = 5
len after  = 7
==ERROR: AddressSanitizer: attempting double-free on 0x1078225e00d0 in thread T0:
    #0 free
    #1 heapless_deque_make_contiguous_back_cursor::main

Suggested fix

Set back to the end of the moved elements instead of 0 (src/deque.rs):

    self.front = back;
    // the elements now span `front..front + len`, not up to the end of the buffer
    self.back = self.to_physical_index(len);

to_physical_index wraps at the capacity, so when the elements end at the last slot it still gives 0, as before.

I applied this to a copy of 0.9.3. The PoC prints len after = 5 with no AddressSanitizer report.

Thank you for your time.

主要言語
Rust
スター
2k
フォーク
253
平均マージ
3日 2時間
マージ済み PR(30日)
1

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

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

rust-embedded/heapless のほかの issue

rust-embedded/heapless の issue をすべて見る

似ている issue

Rust の issue をもっと見る

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

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