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

InsZVA/GCAI: race/judge.go; 130 LoC

Open
#10,471 0 comments 0 reactions 0 assignees View on GitHub
fresh large
Dominant language
No language data
Stars
0
Forks
0
PR merge metrics
PR metrics pending

Description

Found a possible issue in [InsZVA/GCAI](https://www.github.com/InsZVA/GCAI) at [race/judge.go](https://github.com/InsZVA/GCAI/blob/7fda4190a668a72905a3a38bd57a816091f652f5/race/judge.go#L27-L156)

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 task used in defer or goroutine at line 139

[Click here to see the code in its original context.](https://github.com/InsZVA/GCAI/blob/7fda4190a668a72905a3a38bd57a816091f652f5/race/judge.go#L27-L156)

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

```go
for task := range judge.tasks {
if task.Started != nil {
task.Started()
}

gameInfo, err := GetGameInfo(task.GameId)
if err != nil {
task.Callback(3)
continue
}

stop := make(chan struct{}, 16)
cmdJudge := exec.Command(gameInfo.JudgePath)
stdinJudge, err := cmdJudge.StdinPipe()
if err != nil {
task.Callback(3)
continue
}
stdoutJudge, err := cmdJudge.StdoutPipe()
if err != nil {
task.Callback(3)
continue
}
stderrJudge := bytes.NewBuffer(make([]byte, 0, 16*1024))
cmdJudge.Stderr = stderrJudge
cmdJudge.Start()

// TODO: 解释性语言的处理
cmdAI1 := exec.Command(task.AI1Path)
cmdAI2 := exec.Command(task.AI2Path)

var result int
var finish bool
go func () {
cmdJudge.Wait()
stop <- struct{}{}
} ()

go func () {
for !finish {
var outLen int16
if binary.Read(stdoutJudge, binary.LittleEndian, &outLen) != nil {
result = 3
stop <- struct{}{}
return
}
ai1in := make([]byte, outLen)
n, e := stdoutJudge.Read(ai1in)
if e != nil || int16(n) != outLen {
result = 3
stop <- struct{}{}
return
}
log.Println("AI1 IN:", string(ai1in))

if binary.Read(stdoutJudge, binary.LittleEndian, &outLen) != nil {
result = 3
stop <- struct{}{}
return
}
ai2in := make([]byte, outLen)
n, e = stdoutJudge.Read(ai2in)
if e != nil || int16(n) != outLen {
result = 3
stop <- struct{}{}
return
}
log.Println("AI2 IN:", string(ai2in))

cmdAI1.Stdin = bytes.NewBuffer(ai1in)
stdoutAI1 := bytes.NewBuffer(make([]byte, 0, 16*1024))
cmdAI1.Stdout = stdoutAI1
cmdAI1.Start()

cmdAI2.Stdin = bytes.NewBuffer(ai2in)
stdoutAI2 := bytes.NewBuffer(make([]byte, 0, 16*1024))
cmdAI2.Stdout = stdoutAI2
cmdAI2.Start()

var ai1done, ai2done bool
go func() {
cmdAI1.Wait()
ai1done = true
}()
go func() {
cmdAI2.Wait()
ai2done = true
}()

nodeStartTime := time.Now()
for nodeStartTime.Add(time.Duration(gameInfo.TimeLimit) * time.Millisecond).After(time.Now()) {
// TODO: Check RAM Usage
time.Sleep(10 * time.Millisecond)
if ai1done && ai2done {
break
}
}
cmdAI1.Process.Kill()
cmdAI2.Process.Kill()

ai1out := stdoutAI1.Bytes()
n, e = stdoutAI1.Read(ai1out)
binary.Write(stdinJudge, binary.LittleEndian, int16(len(ai1out)))
stdinJudge.Write(ai1out)
log.Println("AI1 OUT:", string(ai1out))

ai2out := stdoutAI2.Bytes()
n, e = stdoutAI2.Read(ai2out)
binary.Write(stdinJudge, binary.LittleEndian, int16(len(ai2out)))
stdinJudge.Write(ai2out)
log.Println("AI2 OUT:", string(ai2out))

cmdAI1 = exec.Command(task.AI1Path)
cmdAI2 = exec.Command(task.AI2Path)
}
} ()

select {
case <-time.After(100 * time.Second):
cmdJudge.Process.Kill()
case <-stop:
}
finish = true
if stderrJudge.Len() != 0 {
fmt.Fscanf(stderrJudge, "%d\n", &result)
}
if task.Callback != nil {
task.Callback(result)
}
}

```

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

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.