Skip to content

fix: reject overlong Rabin-Karp patterns - #3261

Open
devam007 wants to merge 1 commit into
TheAlgorithms:masterfrom
devam007:codex/rabin-karp-pattern-length
Open

devam007 wants to merge 1 commit into
TheAlgorithms:masterfrom
devam007:codex/rabin-karp-pattern-length

Conversation

@devam007

Copy link
Copy Markdown

Description of Change

Return -1 before hashing when the pattern is longer than the text. Previously,
the initial hash accessed characters beyond the text, and the unsigned length
subtraction could underflow. Add the required cstdint header for the existing
int64_t declarations and four assertions for overlong, empty and exact-match
inputs. The hash algorithm and other behavior are unchanged.

Validation

GCC 15.2 / C++17: 16,066 reference comparisons pass against std::string::find,
including exhaustive binary-alphabet texts through length seven and patterns
through length five. The overlong-pattern reproduction fails on the base with
libstdc++ assertions enabled. The CMake/Ninja target, source assertions and
git diff --check pass. Existing signedness warnings remain. This patch does not
redesign integer hashing or establish safety for arbitrarily long patterns.
Remote CI, clang-format and clang-tidy were not run locally.

  • Read contribution guidelines and kept the change to one algorithm file
  • Added regression assertions and verified existing examples
  • Included the header needed by the existing integer type

This branch has not been deployed

No deployments
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.

1 participant