Protect against empty address list, refactor error handling

Added a condition to prevent assignment of an empty address list to the 'HeaderFrom' scenario. This eliminates potential runtime errors when trying to access an nonexistent element of a slice.
Adjusted error handling in the sendmail execution part by reusing the 'err' variable, promoting cleaner, more readable code.
This commit is contained in:
Winni Neessen 2023-11-20 18:36:19 +01:00
parent cdc8c37309
commit 468e1e2b2c
Signed by: wneessen
GPG key ID: 5F3AF39B820C119D

10
msg.go
View file

@ -287,7 +287,9 @@ func (m *Msg) SetAddrHeader(h AddrHeader, v ...string) error {
}
switch h {
case HeaderFrom:
m.addrHeader[h] = []*mail.Address{al[0]}
if len(al) > 0 {
m.addrHeader[h] = []*mail.Address{al[0]}
}
default:
m.addrHeader[h] = al
}
@ -1001,7 +1003,7 @@ func (m *Msg) WriteToSendmailWithContext(ctx context.Context, sp string, a ...st
}
// Start the execution and write to STDIN
if err := ec.Start(); err != nil {
if err = ec.Start(); err != nil {
return fmt.Errorf("could not start sendmail execution: %w", err)
}
_, err = m.WriteTo(si)
@ -1012,7 +1014,7 @@ func (m *Msg) WriteToSendmailWithContext(ctx context.Context, sp string, a ...st
}
// Close STDIN and wait for completion or cancellation of the sendmail executable
if err := si.Close(); err != nil {
if err = si.Close(); err != nil {
return fmt.Errorf("failed to close STDIN pipe: %w", err)
}
@ -1025,7 +1027,7 @@ func (m *Msg) WriteToSendmailWithContext(ctx context.Context, sp string, a ...st
return fmt.Errorf("sendmail command failed: %s", string(serr))
}
if err := ec.Wait(); err != nil {
if err = ec.Wait(); err != nil {
return fmt.Errorf("sendmail command execution failed: %w", err)
}