Fix potential panic from using host before error check in GetP2PNode#24
Merged
Fix potential panic from using host before error check in GetP2PNode#24
Conversation
Merged
Co-authored-by: xzyaoi <10499297+xzyaoi@users.noreply.github.com>
Co-authored-by: xzyaoi <10499297+xzyaoi@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] WIP Address feedback from review on pull request #22
Fix potential panic from using host before error check in GetP2PNode
Feb 12, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a potential panic in GetP2PNode by ensuring newHost() errors are handled before dereferencing/using the returned host.
Changes:
- Move the
newHost()error check to occur beforehost.ID()is accessed. - Only set
MyIDandP2PNodeafter confirmingerr == nil. - Remove unused indirect Go module dependencies (tidy
go.mod/go.sum).
Reviewed changes
Copilot reviewed 2 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/internal/protocol/host.go |
Prevents nil-host dereference by checking err before accessing host.ID(). |
src/go.mod |
Drops unused indirect dependencies. |
src/go.sum |
Removes corresponding unused checksums. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
56
to
+62
| host, err := newHost(ctx, seedInt, ds) | ||
| MyID = host.ID().String() | ||
| P2PNode = &host | ||
| if err != nil { | ||
| common.Logger.Error("Error while creating P2P node: ", err) | ||
| os.Exit(1) | ||
| } | ||
| MyID = host.ID().String() | ||
| P2PNode = &host |
There was a problem hiding this comment.
Consider adding a regression test for this nil-host panic scenario (i.e., when newHost returns (nil, err)). Because GetP2PNode calls os.Exit(1) on error, it’s hard to unit test as-is; a small refactor (e.g., injecting an exit function or returning the error instead of exiting) would make this behavior testable and prevent future regressions.
xzyaoi
added a commit
that referenced
this pull request
Feb 12, 2026
* fix #21 * Update src/entry/cmd/root.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Xiaozhe Yao <xiaozhe.yaoi@gmail.com> * Update src/internal/protocol/host.go Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Xiaozhe Yao <xiaozhe.yaoi@gmail.com> * Fix potential panic from using host before error check in GetP2PNode (#24) * Initial plan * Fix error handling in GetP2PNode - check err before using host Co-authored-by: xzyaoi <10499297+xzyaoi@users.noreply.github.com> * Complete fix for error handling review feedback Co-authored-by: xzyaoi <10499297+xzyaoi@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: xzyaoi <10499297+xzyaoi@users.noreply.github.com> --------- Signed-off-by: Xiaozhe Yao <xiaozhe.yaoi@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
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.
Addresses review feedback from PR #22:
host.ID().String()was called before checking the error returned bynewHost(), which would panic ifnewHostreturns nil host on error.Changes:
newHost()callMyIDandP2PNodewhenerr == nil💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.