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

ruckstack/ruckstack: server/system_control/internal/server/containerd/containerd.go; 142 LoC

Open
#16,966 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 [ruckstack/ruckstack](https://www.github.com/ruckstack/ruckstack) at [server/system_control/internal/server/containerd/containerd.go](https://github.com/ruckstack/ruckstack/blob/5cb6fa011fad49917eaa0274556f62fc37936ccb/server/system_control/internal/server/containerd/containerd.go#L93-L234)

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 untarDir used in defer or goroutine at line 136

[Click here to see the code in its original context.](https://github.com/ruckstack/ruckstack/blob/5cb6fa011fad49917eaa0274556f62fc37936ccb/server/system_control/internal/server/containerd/containerd.go#L93-L234)

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

```go
for _, untarDir := range untarDirs {
importedInfoPath := untarDir + "/imported.info"
importDirectory := false

manifestStat, err := os.Stat(untarDir + "/manifest.json")
if err == nil {
importedStat, err := os.Stat(importedInfoPath)
if err == nil {
if importedStat.ModTime().After(manifestStat.ModTime()) {
logger.Printf("Directory %s has already been imported", untarDir)
importDirectory = false
} else {
logger.Printf("Directory %s has changed since the last time it was imported", untarDir)
importDirectory = true
}
} else {
if err == os.ErrNotExist {
logger.Printf("Directory %s has not been imported before", untarDir)
importDirectory = true
} else {
logger.Printf("Cannot check imported.info file: %s", err)
importDirectory = true
}
}
} else {
if err == os.ErrNotExist {
logger.Printf("Invalid untar dir %s. No manifest.json file", untarDir)
continue
} else {
logger.Printf("Cannot check manifest.json file: %s", err)
importDirectory = true
}
}

if !importDirectory {
continue
}

pipeReader, pipeWriter := io.Pipe()

tarWriter := tar.NewWriter(pipeWriter)

go func() {
if err := filepath.Walk(untarDir, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}

relativePath, err := filepath.Rel(untarDir, path)
if err != nil {
return err
}

file, err := os.Open(path)
if err != nil {
return err
}

defer file.Close()

if strings.HasSuffix(relativePath, "layer.tar.gz") {
_, _ = file.Seek(-4, 2)

sizeBuffer := new(bytes.Buffer)
written, err := io.Copy(sizeBuffer, file)
if err != nil {
return err
}
if written != 4 {
return fmt.Errorf("Didn't read size correctly")
}
uncompressedLength := binary.LittleEndian.Uint32(sizeBuffer.Bytes())

_, _ = file.Seek(0, 0)
gzipReader, err := gzip.NewReader(file)
if err != nil {
return err
}

header := &tar.Header{
Name: strings.Replace(relativePath, ".tar.gz", ".tar", 1),
ModTime: info.ModTime(),
Size: int64(uncompressedLength),
Mode: 0644,
}

err = tarWriter.WriteHeader(header)
if err != nil {
return err
}

_, err = io.Copy(tarWriter, gzipReader)
if err != nil {
return err
}
gzipReader.Close()
} else {
header := &tar.Header{
Name: relativePath,
Size: info.Size(),
ModTime: info.ModTime(),
Mode: 0644,
}

err = tarWriter.WriteHeader(header)
if err != nil {
return err
}

_, err = io.Copy(tarWriter, file)
if err != nil {
return err
}
}

return nil
}); err != nil {
logger.Fatal(err)
}

tarWriter.Close()
pipeWriter.Close()

}()

logger.Printf("Importing images in %s...", untarDir)
images, err := containerdClient.Import(ctxContainerD, pipeReader, containerd.WithAllPlatforms(true))
if err != nil {
return err
}

for _, image := range images {
logger.Printf("Imported %s", image.Name)
}

err = ioutil.WriteFile(importedInfoPath, []byte("Import: SUCCESSFUL"), 0644)
if err != nil {
logger.Printf("Cannot write %s: %s", importedInfoPath, err)
}

logger.Printf("Importing images in %s...DONE", untarDir)
}

```

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: 5cb6fa011fad49917eaa0274556f62fc37936ccb

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.