Skip to content

Harden Search::Do state transitions to avoid crashes#5

Merged
WJiangzhi merged 1 commit into
masterfrom
codex/locate-crash-issue-on-mobile
Mar 3, 2026
Merged

Harden Search::Do state transitions to avoid crashes#5
WJiangzhi merged 1 commit into
masterfrom
codex/locate-crash-issue-on-mobile

Conversation

@WJiangzhi
Copy link
Copy Markdown
Member

Motivation

  • A crash can occur when Search::Do indexes trie[state].next[c] while transitions are still -1 (for example if Build() was not called or initialization/order issues), which can lead to out-of-bounds access and app crashes (observed when calling GrenadeSearcher.Do(ClassName)).

Description

  • Add an early return when the trie is empty by checking if (trie.empty()) in Search::Do to avoid invalid access on uninitialized structures.
  • Replace the direct state = trie[state].next[c] with a defensive transition that walks the fail chain while next[c] == -1 and falls back to the root if no valid transition exists to prevent indexing trie[-1].
  • Preserve existing match emission logic and overall matching behavior when the automaton has been correctly built.

Testing

  • Ran an automated build with cmake -S /workspace/ZACLib -B /workspace/ZACLib/build && cmake --build /workspace/ZACLib/build which completed successfully and produced the library and example binaries.

Codex Task

@WJiangzhi WJiangzhi added the codex Created by Codex AI label Mar 3, 2026 — with ChatGPT Codex Connector
@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, 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 Search::Do function by enhancing its robustness against uninitialized or improperly built Aho-Corasick automaton states. The changes ensure that the search mechanism gracefully handles cases where transitions might be invalid, preventing out-of-bounds memory access and improving overall stability without altering the core matching behavior for correctly built automata.

Highlights

  • Crash Prevention: Implemented an early return in Search::Do when the trie is empty to prevent invalid access to uninitialized structures.
  • State Transition Hardening: Replaced direct state transitions with a defensive mechanism that utilizes the fail chain and falls back to the root state (0) when next[c] is -1, avoiding out-of-bounds access.
Changelog
  • ZACLib/ZACLib.cpp
    • Added an early return if the trie is empty to prevent accessing an uninitialized structure.
    • Modified the state transition logic within Search::Do to defensively navigate the fail chain and return to the root state if a direct transition is invalid, thereby preventing crashes from trie[-1] access.
Activity
  • No specific activity (comments, reviews, progress) has been recorded for this pull request yet.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

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.

Comment thread ZACLib/ZACLib.cpp
Comment on lines +230 to +237
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;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

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;
            }

@WJiangzhi WJiangzhi merged commit de8725c into master Mar 3, 2026
3 checks passed
WJiangzhi added a commit that referenced this pull request Mar 3, 2026
Harden Search::Do state transitions to avoid crashes
@WJiangzhi WJiangzhi deleted the codex/locate-crash-issue-on-mobile branch March 6, 2026 04:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

codex Created by Codex AI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant