Harden Search::Do state transitions to avoid crashes#5
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses potential application crashes in the Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request hardens the Search::Do method to prevent crashes when the trie is not fully initialized, for example when Build() has not been called. The changes add a check for an empty trie and implement a safe state transition logic that follows fail links. This correctly prevents out-of-bounds access. I've suggested a minor simplification to the state transition logic to improve conciseness and readability, but the current implementation is correct.
| while (state != 0 && trie[state].next[c] == -1) { | ||
| state = trie[state].fail; | ||
| } | ||
| if (trie[state].next[c] != -1) { | ||
| state = trie[state].next[c]; | ||
| } else { | ||
| state = 0; | ||
| } |
There was a problem hiding this comment.
The logic to handle state transitions is correct and successfully hardens the function against crashes. However, the implementation can be made more concise. The current while loop followed by an if-else block can be simplified into a more standard pattern for Aho-Corasick state transitions. This improves readability and maintainability.
while (trie[state].next[c] == -1 && state != 0) {
state = trie[state].fail;
}
state = trie[state].next[c];
if (state == -1) {
state = 0;
}Harden Search::Do state transitions to avoid crashes
Motivation
Search::Doindexestrie[state].next[c]while transitions are still-1(for example ifBuild()was not called or initialization/order issues), which can lead to out-of-bounds access and app crashes (observed when callingGrenadeSearcher.Do(ClassName)).Description
if (trie.empty())inSearch::Doto avoid invalid access on uninitialized structures.state = trie[state].next[c]with a defensive transition that walks thefailchain whilenext[c] == -1and falls back to the root if no valid transition exists to prevent indexingtrie[-1].Testing
cmake -S /workspace/ZACLib -B /workspace/ZACLib/build && cmake --build /workspace/ZACLib/buildwhich completed successfully and produced the library and example binaries.Codex Task