Skip to content

[Code scan] Validate HopDict diagonal data before mutating state #19

Description

@njzjz

This issue comes from a Codex global scan of deepmodeling/tbplas at commit 4d3652b.

Severity: Medium

HopDict.__setitem__() copies the matrix into _hoppings before checking whether (0, 0, 0) contains forbidden diagonal onsite terms. If _check_diag() raises and the caller catches the exception, the invalid matrix remains stored in the object.

Code reference:

# Set hopping matrix
# We copy the hopping matrix to avoid changing it accidentally.
self._hoppings[rn] = hop_mat.copy()
# Check for diagonal terms
if rn == (0, 0, 0):
self._check_diag()

Suggested fix: validate the candidate matrix before assigning it to _hoppings, or roll back the assignment when validation fails.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status
    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions