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

miguelmota/streamhut: pkg/wsserver/wsserver.go; 92 LoC

Open
#14,566 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 [miguelmota/streamhut](https://www.github.com/miguelmota/streamhut) at [pkg/wsserver/wsserver.go](https://github.com/miguelmota/streamhut/blob/c3b706401dadf6d2fe3db2140e5e1a65cc4df942/pkg/wsserver/wsserver.go#L191-L282)

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 client used in defer or goroutine at line 224

[Click here to see the code in its original context.](https://github.com/miguelmota/streamhut/blob/c3b706401dadf6d2fe3db2140e5e1a65cc4df942/pkg/wsserver/wsserver.go#L191-L282)

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

```go
for _, client := range clients {
connections := []string{}
for _, connection := range clients {
if connection.ID != client.ID {
connections = append(connections, connection.ID)
}
}

sm := &ServerMessage{
Root: &ServerMessageData{
Data: &ServerMessageConnections{
ConnectionID: client.ID,
Connections: connections,
},
},
}

msg, err := json.Marshal(sm)
if err != nil {
return err
}

if err := client.Write(msg); err != nil {
return err
}

go func() {
if w.db == nil {
return
}

// TODO: read from ws settings
play := false
logs := w.db.ReadStreamLogs(client.Channel)
for i, vLog := range logs {
mime := "shell"
if play {
var date1 int64
if i > 0 {
date1 = logs[i-1].CreatedAt.Unix()
}

date2 := logs[i].CreatedAt.Unix()
if i == 0 {
date1 = date2
}

elapsed := date2 - date1
time.Sleep(time.Duration(elapsed) * time.Second)
/*

let date1 = null
if (i > 0) {
date1 = toUnix(logs[i-1].created_at)
}
let date2 = toUnix(logs[i].created_at)
if (i == 0) {
date1 = date2
}
let elapsed = date2 - date1
await sleep(elapsed*1e3)
*/
}

if mime != "shell-stdin" {
bufferWithMime := byteutil.BufferWithMime(vLog.Data, mime)
if err = client.Write(bufferWithMime); err != nil {
errCh <- err
return
}
}
}
}()

go func(client *Conn) {
if w.db == nil {
return
}

messages := w.db.ReadStreamMessages(client.Channel)
for _, vLog := range messages {
if vLog.Mime != "shell-stdin" {
bufferWithMime := byteutil.BufferWithMime(vLog.Message, vLog.Mime)
if err = client.Write(bufferWithMime); err != nil {
errCh <- err
return
}
}
}
}(client)

}

```

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: c3b706401dadf6d2fe3db2140e5e1a65cc4df942

Contributor guide

No contributing guide indexed for this repository

Research direction

Read pkg/wsserver/wsserver.go around lines 191-282, starting with the clients range loop and the goroutines that use client. Determine whether the analyzer finding affects which connection each goroutine uses; done means recording a justified Bug, Mitigated, or Desirable Behavior classification.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
backend, networking
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.