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

mraron/njudge: utils/problems/tasktype/communication/communication.go; 141 LoC

Open
#15,009 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 [mraron/njudge](https://www.github.com/mraron/njudge) at [utils/problems/tasktype/communication/communication.go](https://github.com/mraron/njudge/blob/8b061ce259c301fe7f7e9004553d31bee142c39b/utils/problems/tasktype/communication/communication.go#L113-L253)

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 tc used in defer or goroutine at line 181

[Click here to see the code in its original context.](https://github.com/mraron/njudge/blob/8b061ce259c301fe7f7e9004553d31bee142c39b/utils/problems/tasktype/communication/communication.go#L113-L253)

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

```go
for _, tc := range g.Testcases {
testNotifier <- strconv.Itoa(tc.Index)
statusNotifier <- ans

if dependenciesOK(g.Dependencies) {
testLocation, answerLocation := tc.InputPath, tc.AnswerPath

testFile, err := os.Open(testLocation)
if err != nil {
tc.VerdictName = problems.VERDICT_XX
return ans, err
}

err = interactorSandbox.CreateFile("inp", testFile)
if err != nil {
tc.VerdictName = problems.VERDICT_XX
return ans, err
}

stdout := &bytes.Buffer{}

answerFile, err := os.Open(answerLocation)
if err != nil {
tc.VerdictName = problems.VERDICT_XX
return ans, err
}

answerContents, err := ioutil.ReadAll(answerFile)
if err != nil {
tc.VerdictName = problems.VERDICT_XX
return ans, err
}

var res language.Status

os.Remove("/tmp/fifo1"+interactorSandbox.Id())
os.Remove("/tmp/fifo2"+interactorSandbox.Id())

err = syscall.Mkfifo(filepath.Join("/tmp", "fifo1"+interactorSandbox.Id()), 0766)
if err != nil {
tc.VerdictName = problems.VERDICT_XX
return ans, err
}

err = syscall.Mkfifo(filepath.Join("/tmp", "fifo2"+interactorSandbox.Id()), 0766)
if err != nil {
tc.VerdictName = problems.VERDICT_XX
return ans, err
}

fifo1, err := os.OpenFile(filepath.Join("/tmp", "fifo1"+interactorSandbox.Id()), os.O_RDWR, 0766)
if err != nil {
tc.VerdictName = problems.VERDICT_XX
return ans, err
}
defer fifo1.Close()

fifo2, err := os.OpenFile(filepath.Join("/tmp", "fifo2"+interactorSandbox.Id()), os.O_RDWR, 0766)
if err != nil {
tc.VerdictName = problems.VERDICT_XX
return ans, err
}
defer fifo2.Close()

done := make(chan int, 1)

go func() {
// @TODO check res and err of interactor
interactorSandbox.Stdin(fifo1).Stdout(fifo2).Stderr(os.Stderr).TimeLimit(tc.TimeLimit).MemoryLimit(tc.MemoryLimit).Run("interactor inp out", true)

done<-1
}()

res, err = lang.Run(s, bytes.NewReader(binaryContents), fifo2, fifo1, tc.TimeLimit, tc.MemoryLimit)
<- done

if err != nil {
tc.VerdictName = problems.VERDICT_XX
return ans, err
}

if res.Verdict == language.VERDICT_OK {
expectedOutput := string(answerContents)

tc.OutputPath = filepath.Join(interactorSandbox.Pwd(), "out")

err = jinfo.Check(&tc)

tc.Output = truncate(stdout.String())
tc.ExpectedOutput = truncate(expectedOutput)
tc.MemoryUsed = res.Memory
tc.TimeSpent = res.Time

testset.Testcases = append(testset.Testcases, tc)
group.Testcases = append(group.Testcases, tc)

if err == nil {
if tc.VerdictName == problems.VERDICT_WA || tc.VerdictName == problems.VERDICT_PE {
ac = false
if skeleton.FeedbackType != problems.FEEDBACK_IOI {
return ans, nil
}
}
} else {
return ans, err
}
} else {
ac = false

curr := tc
curr.Testset = ts.Name
switch res.Verdict {
case language.VERDICT_RE:
curr.VerdictName = problems.VERDICT_RE
case language.VERDICT_XX:
curr.VerdictName = problems.VERDICT_XX
case language.VERDICT_ML:
curr.VerdictName = problems.VERDICT_ML
case language.VERDICT_TL:
curr.VerdictName = problems.VERDICT_TL
}

curr.Group = g.Name
curr.MemoryUsed = res.Memory
curr.TimeSpent = res.Time
curr.Score = 0
curr.Output = truncate(stdout.String()) //now it's stderr
curr.ExpectedOutput = truncate(string(answerContents))

testset.Testcases = append(testset.Testcases, curr)
group.Testcases = append(group.Testcases, curr)

if skeleton.FeedbackType != problems.FEEDBACK_IOI {
return ans, nil
}
}
} else {
group.Testcases = append(group.Testcases, tc)
testset.Testcases = append(testset.Testcases, tc)
}
}

```

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: 8b061ce259c301fe7f7e9004553d31bee142c39b

Contributor guide

No contributing guide indexed for this repository

Research direction

Read utils/problems/tasktype/communication/communication.go around lines 113-253 at commit 8b061ce259c301fe7f7e9004553d31bee142c39b, beginning with the range loop and the reported line 181. Trace the deferred file closes and goroutine usage for each testcase, then verify the analyzer no longer reports this finding and that communication judging still completes correctly.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.