dao: fix InitTx returning nil error on Begin failure#2720
Open
nankingjing wants to merge 1 commit into
Open
Conversation
nankingjing
requested review from
N3kox,
fanlv,
hi-pender,
junwen-lee,
liuyunchao-1998,
lvxinyu-1117,
mrh997 and
shentongmartin
as code owners
July 11, 2026 10:42
Author
Review: fix/init-tx-nil-error (#2720)Verdict: LGTM Same class of bug as #2719: named-return-variable shadowing a real error. The function signature is The fix correctly returns This is a minimal, correct one-line fix. No test included but the logic is self-evident. Merge-ready. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
In
InitTx(), whendao.DB.Begin()fails (rare, e.g. connection error), the function returns(nil, nil)instead of(nil, tx.Error)because it returns the named returnerrwhich is uninitialized (always nil).Root cause
The named return
erris never assigned a value in this function. Whentx.Erroris non-nil, line 88 returns(nil, nil)— the caller receives a nil error, proceeds to use the niltx, and panics with a nil pointer dereference.Fix
Return
tx.Errorinstead oferron the error path:This is a pure Go language semantics bug — the named-error pattern works when
erris assigned upstream, but it is not in this function. Exact same class as the CSV/user.go nil-error bugs fixed previously.Verification
Verified by reading the source; no Go toolchain available on this machine. The bug is provable by static analysis:
erris a named return of typeerror, default-initialized tonil, and never assigned in the function body.File changed
backend/domain/knowledge/internal/dal/dao/knowledge.goline 88