Skip to content

fix: fix nil pointer dereference error in error handler #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion internal/protocol/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ func (p *Protocol) handleCancelledNotification(notification *transport.BaseJSONR
}

func (p *Protocol) handleResponse(response *transport.BaseJSONRPCResponse, errResp *transport.BaseJSONRPCError) {
var id = response.Id
var id transport.RequestId
Copy link
Preview

Copilot AI Apr 27, 2025

Choose a reason for hiding this comment

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

In the error branch of handleResponse, 'id' is not initialized with response.Id, potentially leading to a zero value being used later. Consider assigning id = response.Id in both branches to ensure consistent behavior.

Copilot uses AI. Check for mistakes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

response.Id causes nil pointer dereference error

var result interface{}
var err error

Expand All @@ -354,6 +354,7 @@ func (p *Protocol) handleResponse(response *transport.BaseJSONRPCResponse, errRe
} else {
// Parse the response
result = response.Result
id = response.Id
}

p.mu.RLock()
Expand Down
45 changes: 45 additions & 0 deletions internal/protocol/protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,51 @@ func TestProtocol_RequestHandler(t *testing.T) {
}
}

func TestProtocol_RequestHandler_ErrorResponse(t *testing.T) {
p := NewProtocol(nil)
tr := testingutils.NewMockTransport()

if err := p.Connect(tr); err != nil {
t.Fatalf("Connect failed: %v", err)
}

// Register request handler
handlerCalled := false
p.SetRequestHandler("test_method", func(ctx context.Context, req *transport.BaseJSONRPCRequest, extra RequestHandlerExtra) (transport.JsonRpcBody, error) {
handlerCalled = true
return nil, errors.New("sample error")
})

// Simulate incoming request
tr.SimulateMessage(transport.NewBaseMessageRequest(&transport.BaseJSONRPCRequest{
Jsonrpc: "2.0",
Method: "test_method",
Params: json.RawMessage(`{"param": "value"}`),
}))

// Give some time for handler to be called
time.Sleep(50 * time.Millisecond)

if !handlerCalled {
t.Error("Request handler was not called")
}

// Check response
msgs := tr.GetMessages()
if len(msgs) != 1 {
t.Fatalf("Expected 1 message, got %d", len(msgs))
}

response := msgs[0]
if response.Type != transport.BaseMessageTypeJSONRPCErrorType {
t.Fatal("Message is not a response")
}

if string(response.JsonRpcError.Error.Message) != "sample error" {
t.Errorf("Expected result 'sample error', got %v", string(response.JsonRpcError.Error.Message))
}
}

// TestProtocol_NotificationHandler tests the handling of incoming notifications.
// This is important for asynchronous events and status updates.
// It verifies:
Expand Down