From 8c4eb62360af707726f414ce9ea824e1dbda9de9 Mon Sep 17 00:00:00 2001 From: Alysson Ribeiro <15274059+sonalys@users.noreply.github.com> Date: Wed, 6 Nov 2024 10:20:59 +0100 Subject: [PATCH 1/3] Fix(close): Access to nil variable causes panic --- client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client.go b/client.go index abb90f4..fbb28f5 100644 --- a/client.go +++ b/client.go @@ -996,7 +996,7 @@ func (c *Client) DialWithContext(dialCtx context.Context) error { // Returns: // - An error if the disconnection fails; otherwise, returns nil. func (c *Client) Close() error { - if !c.smtpClient.HasConnection() { + if c.smtpClient == nil || !c.smtpClient.HasConnection() { return nil } if err := c.smtpClient.Quit(); err != nil { From a5fcb3ae8bd9ea47e932093bfa6ac629a9166412 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Wed, 6 Nov 2024 10:47:31 +0100 Subject: [PATCH 2/3] Add test for closing a nil smtpclient Introduce a unit test to ensure that invoking Close on a nil smtpclient instance returns nil without errors. This enhances the robustness of the client closure functionality. This test accommodates the fix provided with PR #353 --- client_test.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/client_test.go b/client_test.go index 664f0fd..96ebf1f 100644 --- a/client_test.go +++ b/client_test.go @@ -1647,6 +1647,15 @@ func TestClient_Close(t *testing.T) { t.Errorf("close was supposed to fail, but didn't") } }) + t.Run("close on a nil smtpclient should return nil", func(t *testing.T) { + client, err := NewClient(DefaultHost) + if err != nil { + t.Fatalf("failed to create new client: %s", err) + } + if err = client.Close(); err != nil { + t.Errorf("failed to close the client: %s", err) + } + }) } func TestClient_DialWithContext(t *testing.T) { From b4aa414a4dd11049205849d6122e7397f3d357dd Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Wed, 6 Nov 2024 11:22:54 +0100 Subject: [PATCH 3/3] Update doc.go Bump version to v0.5.2 --- doc.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc.go b/doc.go index 88a12ee..6878d86 100644 --- a/doc.go +++ b/doc.go @@ -11,4 +11,4 @@ package mail // VERSION indicates the current version of the package. It is also attached to the default user // agent string. -const VERSION = "0.5.1" +const VERSION = "0.5.2"