Skip to content

fix(searches): avoid int overflow and short-text crash in RabinKarpAlgorithm - #7665

Open
Rishavbhattarai wants to merge 1 commit into
TheAlgorithms:masterfrom
Rishavbhattarai:fix/rabinkarp-overflow
Open

Rishavbhattarai wants to merge 1 commit into
TheAlgorithms:masterfrom
Rishavbhattarai:fix/rabinkarp-overflow

Conversation

@Rishavbhattarai

@Rishavbhattarai Rishavbhattarai commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Two bugs in searches/RabinKarpAlgorithm.search:

  1. The rolling hash is an int, so ALPHABET_SIZE * hash overflows for large primes. With the common prime 1_000_000_007, I checked 5,000 random lowercase texts against a pattern taken from each one, and 1,110 returned the wrong index. Example: search("jggw", "nzdjsjggwx", 1_000_000_007) returns -1 instead of 5. The hashes are now long.
  2. When the pattern is longer than the text, the first hash loop reads past the end of the text: search("ABC", "AB", 101) throws StringIndexOutOfBoundsException. It now returns -1.

Both cases are added to the existing parameterized test and fail on master.

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized it.
  • All filenames are in PascalCase.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations. (No new algorithm.)
  • All new algorithms include a corresponding test class that validates their functionality. (Regression test added.)
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.20%. Comparing base (96ed2d7) to head (6709998).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7665      +/-   ##
============================================
- Coverage     82.20%   82.20%   -0.01%     
  Complexity     8397     8397              
============================================
  Files           843      843              
  Lines         26133    26135       +2     
  Branches       5097     5098       +1     
============================================
+ Hits          21482    21483       +1     
  Misses         3859     3859              
- Partials        792      793       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Rishavbhattarai
Rishavbhattarai marked this pull request as ready for review October 27, 2026 03:09
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