-
Notifications
You must be signed in to change notification settings - Fork 18
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
Drop some unsafe
s - the compiler now optimizes equivalent safe code
#43
Merged
Merged
Changes from 2 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
1a810db
Drop unsafe loops. On Rust 1.27 performance degradation is within mea…
Shnatsel 488db57
Exit earlier on invalid run length, without leaving behind a buffer w…
Shnatsel a5e29c8
Fix formatting
Shnatsel bc3916d
Fix formatting in one more place
Shnatsel 3715c4c
Add semicolon
Shnatsel 11c73f2
run_len_dist: validate that dist is not 0. Fixes information disclosu…
Shnatsel 8f22601
::std::intrinsics now requires 'core_intrinsics' feature, not 'core'
Shnatsel 4e5cc94
Remove use of 'abort' intrinsic now that panic!() is optimized by the…
Shnatsel dd20d3e
Remove 'unstable' feature from Cargo.toml
Shnatsel d56abee
Replace panic!() with unreachable!(). Thank you @hauleth for the advice
Shnatsel 61c2c1c
Revert "Remove 'unstable' feature from Cargo.toml"
Shnatsel d476136
Add an assert() before the last remaining unsafe {}; no performance p…
Shnatsel fdee4de
Add another assert against potentially reading from uninitialized memory
Shnatsel 9c8f582
Add a comment explaining why `if dist < 1` check is there
Shnatsel a2955c8
Add an assert for dist correctness right before it's used. Surprising…
Shnatsel f4418ea
More comments warning the unwary about the one and only unsafe block
Shnatsel baf9c7e
Drop a wall of text in a comment, it is not helpful. There is an asse…
Shnatsel a36a517
Explain what each assert actually does
Shnatsel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,8 +65,6 @@ | |
//! } | ||
//! ``` | ||
|
||
#![cfg_attr(feature = "unstable", feature(core_intrinsics))] | ||
|
||
extern crate adler32; | ||
|
||
use std::cmp; | ||
|
@@ -182,16 +180,6 @@ struct BitStream<'a> { | |
state: BitState, | ||
} | ||
|
||
// Use this instead of triggering a panic (that will unwind). | ||
#[cfg(feature = "unstable")] | ||
fn abort() -> ! { | ||
unsafe { ::std::intrinsics::abort() } | ||
} | ||
#[cfg(not(feature = "unstable"))] | ||
fn abort() -> ! { | ||
panic!() | ||
} | ||
|
||
#[cfg(debug)] | ||
macro_rules! debug { ($($x:tt)*) => (println!($($x)*)) } | ||
#[cfg(not(debug))] | ||
|
@@ -224,10 +212,7 @@ impl<'a> BitStream<'a> { | |
return false; | ||
} | ||
if n > 8 && self.state.n < n { | ||
if n > 16 { | ||
// HACK(eddyb) in place of a static assert. | ||
abort(); | ||
} | ||
assert!(n <= 16); | ||
if !self.use_byte() { | ||
return false; | ||
} | ||
|
@@ -248,10 +233,7 @@ impl<'a> BitStream<'a> { | |
} | ||
|
||
fn take(&mut self, n: u8) -> Option<u8> { | ||
if n > 8 { | ||
// HACK(eddyb) in place of a static assert. | ||
abort(); | ||
} | ||
assert!(n <= 8); | ||
self.take16(n).map(|v: u16| v as u8) | ||
} | ||
|
||
|
@@ -404,7 +386,7 @@ impl CodeLengthReader { | |
self.result.push(0); | ||
} | ||
} | ||
_ => abort(), | ||
_ => panic!(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't it be better to use |
||
} | ||
} | ||
Ok(true) | ||
|
@@ -689,9 +671,7 @@ impl InflateStream { | |
if (self.pos as usize) < self.buffer.len() { | ||
self.buffer[self.pos as usize] = b; | ||
} else { | ||
if (self.pos as usize) != self.buffer.len() { | ||
abort(); | ||
} | ||
assert_eq!(self.pos as usize, self.buffer.len()); | ||
self.buffer.push(b); | ||
} | ||
self.pos += 1; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this needed at all? I think all it did was disable
unstable
by default.