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

muxinc/vmaf_analyzer: main.go; 93 LoC

Open
#6,462 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 [muxinc/vmaf_analyzer](https://www.github.com/muxinc/vmaf_analyzer) at [main.go](https://github.com/muxinc/vmaf_analyzer/blob/3069a913156e384fa2f69dd64debb6993a9b4fb1/main.go#L223-L315)

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 i used in defer or goroutine at line 262

[Click here to see the code in its original context.](https://github.com/muxinc/vmaf_analyzer/blob/3069a913156e384fa2f69dd64debb6993a9b4fb1/main.go#L223-L315)

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

```go
for i := range userPcts {
effectiveVmafs[i] = make([]float64, len(data.ResolutionPcts))
if i == 0 {
continue
}

// calculate vmaf score resolutions at current bitrate bucket
for j, resUserPct := range data.ResolutionPcts {
cancelCtx, cancelFunc := context.WithCancel(ctx)
curWidth := uint64((j + 1) * 16)
curHeight := widthToHeight(curWidth, videoStream.Width, videoStream.Height)

if curWidth < minVmafResolution || curHeight < minVmafResolution {
fmt.Printf("Skipping resolution %dx%d - its too small for VMAF\n", curWidth, curHeight)
continue
}
if resUserPct == 0.0 {
fmt.Printf("Skipping resolution %dx%d - zero percentage of users watch at this resolution\n", curWidth, curHeight)
continue
}

fmt.Printf("Calculating VMAF score at %dx%d\n", curWidth, curHeight)

// decode reference
var wg sync.WaitGroup
errc := make(chan error, 1)
wg.Add(1)
go func() {
fmt.Printf("Decoding this input: %s\n", mezzanineFile)
if err := ffmpeg.DecodeToWidthAndHeight(cancelCtx, mezzanineFile, mezzanineDecodePath, curWidth, curHeight); err != nil {
fmt.Printf("Error encountered decoding mezzanine:\n%v\n", err)
errc <- err
}
wg.Done()
}()

// decode distorted
wg.Add(1)
go func() {
distoredFile := fmt.Sprintf("variant_%d.ts", i-1)

fmt.Printf("Decoding this input: %s\n", distoredFile)
if err := ffmpeg.DecodeToWidthAndHeight(cancelCtx, distoredFile, distortedDecodePath, curWidth, curHeight); err != nil {
fmt.Printf("Error encountered decoding variant:\n%v\n", err)
errc <- err
}
wg.Done()
}()

// calculate VMAF score
var vmafScore float64
wg.Add(1)
go func() {
var vmafErr error
vmafScore, vmafErr = vmaf.CalculateVMAF(cancelCtx, uint64(i-1), curWidth, curHeight)
if vmafErr != nil {
fmt.Printf("Error encountered calculating vmaf:\n%v\n", vmafErr)
errc <- err
} else if vmafScore < lowVMAFThreshold {
errc <- fmt.Errorf("Low vmaf score detected, most likely due to misconfiguration. Score %f is below threshold %f\n", vmafScore, lowVMAFThreshold)
} else {
fmt.Printf("I calculated vmaf and got this harmonic mean: %f\n", vmafScore)
}

wg.Done()
}()

go func() {
wg.Wait()
close(errc)
}()

hadErr := false
for err := range errc {
if err != nil && !hadErr {
hadErr = true
cancelFunc()
fmt.Printf("Error encountered running VMAF: %v\n", err)
}
}

if hadErr {
fmt.Printf("Error running vmaf calculation, goodbye\n")
return
}
fmt.Println("Oh yeah decode done\n")

// fill in and print effective VMAF score
effectiveVmafs[i][j] = vmafScore
fmt.Printf("%f%% of users have the bitrate to watch this rendition\n", userPcts[i])
fmt.Printf("Of those, %f%% will be watching at the current resolution of %dx%d\n", resUserPct, curWidth, curHeight)
}
}

```

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: 3069a913156e384fa2f69dd64debb6993a9b4fb1

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.