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

kleveross/ormb: pkg/oras/cache/cache.go; 73 LoC

Open
#12,558 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 [kleveross/ormb](https://www.github.com/kleveross/ormb) at [pkg/oras/cache/cache.go](https://github.com/kleveross/ormb/blob/491a73dce0a32b78e1a59354ea9618e4fca58e8b/pkg/oras/cache/cache.go#L88-L160)

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.

> reference to desc is reassigned at line 102

[Click here to see the code in its original context.](https://github.com/kleveross/ormb/blob/491a73dce0a32b78e1a59354ea9618e4fca58e8b/pkg/oras/cache/cache.go#L88-L160)

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

```go
for _, desc := range cache.ociStore.ListReferences() {
if desc.Annotations[ocispec.AnnotationRefName] == r.Name {
r.Exists = true

// Fetch the manifest.
manifestBytes, err := cache.fetchBlob(&desc)
if err != nil {
return &r, err
}
var manifest ocispec.Manifest
err = json.Unmarshal(manifestBytes, &manifest)
if err != nil {
return &r, err
}
r.Manifest = &desc

// TODO(gaocegege): Fetch the config.
r.Config = &manifest.Config
numLayers := len(manifest.Layers)
if numLayers != 1 {
return &r, errors.New(
fmt.Sprintf("manifest does not contain exactly 1 layer (total: %d)", numLayers))
}

// Fetch the content.
var contentLayer *ocispec.Descriptor
for _, layer := range manifest.Layers {
switch layer.MediaType {
case consts.MediaTypeModelContentLayer:
contentLayer = &layer
}
}
if contentLayer == nil {
return &r, errors.New(
fmt.Sprintf(
"manifest does not contain a layer with mediatype %s",
consts.MediaTypeModelContentLayer))
}
if contentLayer.Size == 0 {
return &r, errors.New(
fmt.Sprintf(
"manifest layer with mediatype %s is of size 0",
consts.MediaTypeModelContentLayer))
}
r.ContentLayer = contentLayer
info, err := cache.ociStore.Info(ctx.Context(cache.out, cache.debug), contentLayer.Digest)
if err != nil {
return &r, err
}
r.Size = info.Size
r.Digest = info.Digest
r.CreatedAt = info.CreatedAt
contentBytes, err := cache.fetchBlob(contentLayer)
if err != nil {
return &r, err
}
configBytes, err := cache.fetchBlob(r.Config)
if err != nil {
return &r, err
}
metadata, err := cache.parser.Parse(configBytes)
if err != nil {
return &r, err
}

// TODO(gaocegege): Optimize the memory usage.
r.Model = &model.Model{
Content: contentBytes,
Config: configBytes,
Metadata: metadata,
}
}
}

```

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: 491a73dce0a32b78e1a59354ea9618e4fca58e8b

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.