melody icon indicating copy to clipboard operation
melody copied to clipboard

Check returned errors before deferring Close()

Open riking opened this issue 4 years ago • 1 comments

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.

riking avatar Oct 16 '19 22:10 riking

@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.

CodeDing avatar May 10 '23 06:05 CodeDing