Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BinaryHeap is not exception safe #25842

Closed
bluss opened this issue May 27, 2015 · 5 comments · Fixed by #25856
Closed

BinaryHeap is not exception safe #25842

bluss opened this issue May 27, 2015 · 5 comments · Fixed by #25856
Labels
T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@bluss
Copy link
Member

bluss commented May 27, 2015

BinaryHeap is using zeroed() and may not be exception safe. I.e. it is in an inconsistent state after being recovered after panic. See issue #25662 and others.

Relevant code is BinaryHeap::sift_up, sift_down_range

cc @cmr

@bluss
Copy link
Member Author

bluss commented May 27, 2015

My immediate ideas are one of

  • Use PPYP and set vector length to zero temporarily. Panicing in comparison functions will leak all elements in the heap
  • Use more swaps

@bluss
Copy link
Member Author

bluss commented May 27, 2015

@Gankra
Copy link
Contributor

Gankra commented May 27, 2015

Long ago I suggested two other strategies:

  • A two-pass phase where you do all comparisons first, then once you've found the Final Destination you actually do the swaps (which requires no untrusted code to run).
  • A panic guard which on drop puts the "working" element back in the "uninit" location in the array

@alexcrichton
Copy link
Member

triage: I-nominated, T-libs

@alexcrichton alexcrichton added the T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. label May 27, 2015
@bluss
Copy link
Member Author

bluss commented May 27, 2015

I'm trying gankro's panic guard idea.

bluss pushed a commit to bluss/rust that referenced this issue May 28, 2015
Use a struct called Hole that keeps track of an invalid location
in the vector and fills the hole on drop.

I include a run-pass test that the current BinaryHeap fails, and the new
one passes.

Fixes rust-lang#25842
bluss pushed a commit to bluss/rust that referenced this issue May 28, 2015
Use a struct called Hole that keeps track of an invalid location
in the vector and fills the hole on drop.

I include a run-pass test that the current BinaryHeap fails, and the new
one passes.

Fixes rust-lang#25842
bluss pushed a commit to bluss/rust that referenced this issue May 28, 2015
Use a struct called Hole that keeps track of an invalid location
in the vector and fills the hole on drop.

I include a run-pass test that the current BinaryHeap fails, and the new
one passes.

Fixes rust-lang#25842
bluss pushed a commit to bluss/rust that referenced this issue May 28, 2015
Use a struct called Hole that keeps track of an invalid location
in the vector and fills the hole on drop.

I include a run-pass test that the current BinaryHeap fails, and the new
one passes.

Fixes rust-lang#25842
bors added a commit that referenced this issue May 28, 2015
collections: Make BinaryHeap panic safe in sift_up / sift_down

Use a struct called Hole that keeps track of an invalid location
in the vector and fills the hole on drop.

I include a run-pass test that the current BinaryHeap fails, and the new
one passes.

NOTE: The BinaryHeap will still be inconsistent after a comparison fails. It will
not have the heap property. What we fix is just that elements will be valid
values.

This is actually a performance win -- the new code does not bother to write in `zeroed()`
values in the holes, it just leaves them as they were.

Net result is something like a 5% decrease in runtime for `BinaryHeap::from_vec`. This
can be further improved by using unchecked indexing (I confirmed it makes a difference,
not a surprise with the non-sequential access going on), but let's leave that for another PR.
Safety first 😉 

Fixes #25842
XMPPwocky pushed a commit to XMPPwocky/rust that referenced this issue May 29, 2015
Use a struct called Hole that keeps track of an invalid location
in the vector and fills the hole on drop.

I include a run-pass test that the current BinaryHeap fails, and the new
one passes.

Fixes rust-lang#25842
alexcrichton pushed a commit to alexcrichton/rust that referenced this issue Jun 10, 2015
Use a struct called Hole that keeps track of an invalid location
in the vector and fills the hole on drop.

I include a run-pass test that the current BinaryHeap fails, and the new
one passes.

Fixes rust-lang#25842
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants