`Deque::make_contiguous` sets a wrong `back` for a wrapped, non-full deque: `len()` grows and elements are dropped twice
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 84/100
調査の方向性
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
- Push and pop 4 times on a
Deque<_, 8>, so thatfrontis 4. - Push 5 elements. They wrap into slots 4,5,6,7,0, and the deque is not full.
- 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 を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
rust-embedded/heapless のほかの issue
-
`FromIterator` for `Deque`対応中かも @ollien が 81 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
rust-embedded/heapless#683 ·
-
enhancement
難易度 4/5 3〜5日 初心者へのやさしさ 45/100
rust-embedded/heapless#681 ·
-
enhancement
難易度 3/5 1〜2日 初心者へのやさしさ 55/100
rust-embedded/heapless#653 · コメント 1 件 ·
-
難易度 5/5 1週間以上 初心者へのやさしさ 28/100
rust-embedded/heapless#645 · コメント 5 件 · リアクション 1 件 ·
-
Vec const-nessオープン
難易度 3/5 1〜2日 初心者へのやさしさ 45/100
rust-embedded/heapless#643 · コメント 3 件 ·
rust-embedded/heapless の issue をすべて見る
似ている issue
-
needs triage
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
mozilla-services/syncstorage-rs#2715 ·
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 70/100
-
documentation
難易度 1/5 1時間未満 初心者へのやさしさ 82/100
physicsdolphin/nv-uefi-unlocker#53 · リアクション 4 件 ·
メンテナーはふだん 1 日以内に返信