olahol / melody

:notes: Minimalist websocket framework for Go
BSD 2-Clause "Simplified" License
3.75k stars 366 forks source link

Check returned errors before deferring Close() #60

Open riking opened 5 years ago

riking commented 5 years ago

This pattern is repeated several times in melody_test.go:

conn, err := NewDialer(server.URL)
defer conn.Close()
if err != nil {
    t.Error(err)
    return false
}

However, if there is an error, this is liable to cause a null reference panic.

CodeDing commented 1 year ago

@riking the defer clause should be placed after the if block, the correct code may looks like below:

conn, err := NewDialer(server.URL)
if err != nil {
    t.Error(err)
    return false
}
defer conn.Close()

A good luck to you if it can solve the problem.