github-vet / github-vet/rangeloop-pointer-findings

toorop/tmail: core/smtp_client.go; 75 LoC

Open
#6,622 0 comments 0 reactions 0 assignees View on GitHub
fresh medium
Dominant language
No language data
Stars
0
Forks
0
PR merge metrics
PR metrics pending

Description

Found a possible issue in [toorop/tmail](https://www.github.com/toorop/tmail) at [core/smtp_client.go](https://github.com/toorop/tmail/blob/38218317af63a2f467fbac6cd10931f97be69b40/core/smtp_client.go#L116-L190)

Below is the message reported by the analyzer for this snippet of code. Beware that the analyzer only reports the first
issue it finds, so please do not limit your consideration to the contents of the below message.

> range-loop variable remoteAddr used in defer or goroutine at line 139

[Click here to see the code in its original context.](https://github.com/toorop/tmail/blob/38218317af63a2f467fbac6cd10931f97be69b40/core/smtp_client.go#L116-L190)

Click here to show the 75 line(s) of Go which triggered the analyzer.

```go
for _, remoteAddr := range remoteAddresses {
// IPv4 <-> IPv4 or IPv6 <-> IPv6
if IsIPV4(localIP.String()) != IsIPV4(remoteAddr.IP.String()) {
continue
}

// If during the last 15 minutes we have fail to connect to this host don't try again
if !isRemoteIPOK(remoteAddr.IP.String()) {
Logger.Info("smtp getclient " + remoteAddr.IP.String() + " is marked as KO. I'll dot not try to reach it.")
continue
}

localAddr, err := net.ResolveTCPAddr("tcp", localIP.String()+":0")
if err != nil {
return nil, errors.New("bad local IP: " + localIP.String() + ". " + err.Error())
}

// Dial timeout
connectTimer := time.NewTimer(time.Duration(timeoutBasePerCmd) * time.Second)
done := make(chan error, 1)
var conn net.Conn
var client *smtpClient
go func() {
conn, err = net.DialTCP("tcp", localAddr, &remoteAddr)
if err != nil {
done <- err
return
}
client = &smtpClient{
conn: conn,
timeoutBasePerCmd: timeoutBasePerCmd,
}
client.route = &route
client.text = textproto.NewConn(conn)
_, _, err = client.text.ReadResponse(220)
done <- err
}()

select {
case err = <-done:
if err == nil {
return client, nil
}

//client.text = textproto.NewConn(conn)
// timeout on response
/*connectTimer.Reset(time.Duration(30) * time.Second)
go func() {

client.text = textproto.NewConn(conn)
_, _, err = client.text.ReadResponse(220)
done <- err
}()
select {
case err = <-done:*/

// Timeout
/*case <-connectTimer.C:
conn.Close()
err = errors.New("timeout")
// todo si c'est un timeout pas la peine d'essayer les autres IP locales
if errBolt := setIPKO(remoteAddr.IP.String()); errBolt != nil {
Logger.Error("Bolt - ", errBolt)
}*/

// Timeout
case <-connectTimer.C:
err = errors.New("timeout")
// todo si c'est un timeout pas la peine d'essayer les autres IP locales
if errBolt := setIPKO(remoteAddr.IP.String()); errBolt != nil {
Logger.Error("Bolt - ", errBolt)
}
}
Logger.Info(fmt.Sprintf("deliverd-remote %s - unable to get a SMTP client for %s->%s:%d - %s ", d.ID, localIP, remoteAddr.IP.String(), remoteAddr.Port, err.Error()))
}

```

Leave a reaction on this issue to contribute to the project by classifying this instance as a **Bug** :-1:, **Mitigated** :+1:, or **Desirable Behavior** :rocket:
See the descriptions of the classifications [here](https://github.com/github-vet/rangeclosure-findings#how-can-i-help) for more information.

commit ID: 38218317af63a2f467fbac6cd10931f97be69b40

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.