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

Fix usize overflow #548

Merged
merged 4 commits into from
Oct 7, 2023
Merged

Conversation

suzp1984
Copy link
Contributor

@suzp1984 suzp1984 commented Oct 7, 2023

Fix usize overflow problem when calculate middle of two usize

Description

// left: usize;
// right: usize;
// left <= right;
let middle = (left + right) / 2;

if (left + right) > std::usize::MAX, then it's an overflow bug.

The common solution to calculate the middle value is:

let middle = left + (right - left) / 2;

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • I ran bellow commands using the latest version of rust nightly.
  • I ran cargo clippy --all -- -D warnings just before my last commit and fixed any issue that was found.
  • I ran cargo fmt just before my last commit.
  • I ran cargo test just before my last commit and all tests passed.
  • I checked COUNTRIBUTING.md and my code follows its guidelines.

Please make sure that if there is a test that takes too long to run ( > 300ms), you #[ignore] that or
try to optimize your code or make the test easier to run. We have this rule because we have hundreds of
tests to run; If each one of them took 300ms, we would have to wait for a long time.

@suzp1984 suzp1984 changed the title Fix/usize overflow Fix usize overflow Oct 7, 2023
Copy link
Member

@siriak siriak left a comment

Choose a reason for hiding this comment

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

Looks good, thanks!

@siriak siriak enabled auto-merge (squash) October 7, 2023 13:24
@siriak siriak merged commit 8ca6ae7 into TheAlgorithms:master Oct 7, 2023
4 checks passed
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.

2 participants