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

Guard against usize overflows/underflows and raw pointer undefined behavior #50

Merged
merged 1 commit into from
May 17, 2022

Conversation

arthurprs
Copy link
Contributor

The main change is to change patterns like current_position + something > max_position to max_position - current_position < something, which is overflow safe. Closes #49

@@ -177,7 +177,7 @@ fn count_same_bytes(input: &[u8], cur: &mut usize, source: &[u8], candidate: usi
// compare 4 bytes block
#[cfg(target_pointer_width = "64")]
{
if *cur + 4 <= input_end {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cur and input_end are usize, they are unaffected by the issue

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but since we're dealing with under/overflow style problems I thought it would be best to fix all of them. Theoretically an allocation at the end of the address space could lead to *cur + 4 to overflow.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hehe when compressing 2^32-1 on 32 bit systems

@PSeitz PSeitz merged commit 24e3271 into PSeitz:main May 17, 2022
@PSeitz
Copy link
Owner

PSeitz commented May 17, 2022

Awesome, thanks for the PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Undefined Behavior with dict and pointer arithmetic
2 participants