Skip to content

mssmt: guard nil children in BranchNode.Copy#2119

Open
jtobin wants to merge 1 commit into
lightninglabs:mainfrom
jtobin:mssmt-guard-nil-children
Open

mssmt: guard nil children in BranchNode.Copy#2119
jtobin wants to merge 1 commit into
lightninglabs:mainfrom
jtobin:mssmt-guard-nil-children

Conversation

@jtobin
Copy link
Copy Markdown
Member

@jtobin jtobin commented May 1, 2026

(This was sussed out by #2117, which will require the fix in order for its CI to pass.)

An MS-SMT's BranchNode, which always has two children (it's a branch), has two valid representations:

  1. a full branch, with children materialised, as produced by the in-memory Golang store via pointer traversal, and
  2. a so-called "computed branch," with hash/sum precomputed, but children not yet computed (i.e. they're in the database, or certainly should be, but we haven't fetched them yet), as produced by the SQL backends via one-level-at-a-time fetch.

Copy() should handle both, but previously it assumed the full branch / in-memory representation and dereferenced children unconditionally. That caused panics when using e.g. sqlite3 and postgres backends for the tree store, since the children in the computed branch representation are always nil.

The fix is to just do an explicit nil check, dereferencing child nodes only if they're non-nil. This ensures the BranchNode is faithfully copied, whatever its representation (if we're copying a computed branch, the copy will also be a computed branch).

NewComputedBranch creates BranchNode with nil Left/Right children,
which the DB-backed tree stores return during proof construction.
Copy() previously dereferenced these unconditionally, causing a
panic when running mssmt tests against sqlite3/postgres backends.
@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 resolves a critical issue in the BranchNode.Copy() method within the MS-SMT implementation, preventing panics that occurred when copying "computed branches" where child nodes are intentionally nil. By introducing defensive nil checks, the change ensures that the Copy() operation is robust across different branch representations, particularly those used by SQL backends, thereby improving the stability and reliability of the tree store.

Highlights

  • Fixes Panic in BranchNode.Copy(): Addressed an issue where BranchNode.Copy() would panic when encountering nil child nodes, specifically in "computed branch" representations where children are not yet materialized.
  • Introduces Nil Checks: Implemented explicit nil checks for Left and Right child nodes before attempting to access their NodeHash() and NodeSum() methods.
  • Ensures Correct Copying of Computed Branches: The Copy() method now correctly handles both full and computed branch representations, preserving the nil state of unmaterialized children and preventing crashes with SQL backends.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

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.

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 updates the Copy method for BranchNode to include nil checks for child nodes, preventing potential null pointer dereferences. The reviewer suggests refactoring the logic for creating computed nodes to reduce duplication and improve readability, noting that the current implementation can be simplified.

Comment thread mssmt/node.go
@lightninglabs-deploy
Copy link
Copy Markdown

@Roasbeef: review reminder
@jtobin, remember to re-request review from reviewers when ready

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: 🆕 New

Development

Successfully merging this pull request may close these issues.

2 participants