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

janelia-flyem/dvid: datastore/datastore.go; 107 LoC

Open
#11,037 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 [janelia-flyem/dvid](https://www.github.com/janelia-flyem/dvid) at [datastore/datastore.go](https://github.com/janelia-flyem/dvid/blob/cb4620e65a3cc57fa038c6242abc01b0873f0299/datastore/datastore.go#L635-L741)

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 store used in defer or goroutine at line 656

[Click here to see the code in its original context.](https://github.com/janelia-flyem/dvid/blob/cb4620e65a3cc57fa038c6242abc01b0873f0299/datastore/datastore.go#L635-L741)

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

```go
for alias, store := range stores {
stats := make(StorageStats)
db, ok := store.(storage.OrderedKeyValueGetter)
if !ok {
dvid.Infof("unable to get storage details for store %s: not ordered kv\n", store)
continue
}

wg := new(sync.WaitGroup)
wg.Add(1)
ch := make(chan *storage.KeyValue, 1000)
var numKeys uint64
go func(wg *sync.WaitGroup, ch chan *storage.KeyValue) {
for {
kv := <-ch
if kv == nil {
wg.Done()
return
}
numKeys++
if numKeys%100000 == 0 {
timedLog.Infof("Storage details for store %s, processing key %d", store, numKeys)
}
instanceID, versionID, _, err := storage.DataKeyToLocalIDs(kv.K)
if err != nil {
dvid.Infof("error trying to parse data key %x: %v\n", kv.K, err)
continue
}
// get uuid and repo and find out if it's leaf
d, err := manager.getDataByInstanceID(instanceID)
if err != nil {
dvid.Errorf("got key with instance id %d that has no associated data: %v\n", instanceID, err)
continue
}
leaf, wasSeen := isLeaf[versionID]
if !wasSeen {
uuid, found := manager.versionToUUID[versionID]
if !found {
dvid.Errorf("got key with version %d and no uuid mapping: skipping\n", versionID)
continue
}
repo, found := manager.repos[uuid]
if !found {
dvid.Errorf("got key with version %d, uuid %s, but no repo!\n", versionID, uuid)
continue
}
repo.RLock()
if repo.dag == nil {
dvid.Errorf("repo %s has a nil dag: skipping\n", repo.uuid)
repo.RUnlock()
continue
}
node, found := repo.dag.nodes[versionID]
repo.RUnlock()
if !found {
dvid.Errorf("version %d not found in dag nodes, skipping\n", versionID)
continue
}
if len(node.children) > 0 {
leaf = false
isLeaf[versionID] = false
} else {
leaf = true
isLeaf[versionID] = true
}
}
tk, err := storage.TKeyFromKey(kv.K)
if err != nil {
dvid.Errorf("bad tkey extraction: %v\n", kv.K)
continue
}
if len(tk) == 0 {
dvid.Errorf("bad tkey with no length, skipping key %s\n", kv.K)
continue
}
tkclass := storage.TKeyClass(tk[0])
desc := d.DescribeTKeyClass(tkclass)
dataid := fmt.Sprintf("%s-%s", d.RootUUID(), d.DataName())
istats := stats[dataid]
if istats == nil {
istats = make(map[string]KeyStats)
}
kstats := istats[desc]
if leaf {
kstats.LeafKV++
kstats.LeafBytes += uint64(len(kv.V))
} else {
kstats.IntKV++
kstats.IntBytes += uint64(len(kv.V))
}
istats[desc] = kstats
stats[dataid] = istats
}
}(wg, ch)

minKey, maxKey := storage.DataKeyRange()
keysOnly := false
if err = db.RawRangeQuery(minKey, maxKey, keysOnly, ch, nil); err != nil {
return nil, err
}
wg.Wait()

timedLog.Infof("Finished storage details for store %s: %d keys", store, numKeys)
dvid.Infof("\nStore %d stats:\n", store)
dvid.Infof("%s\n", stats.String())
statsByStore[string(alias)] = stats
}

```

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

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.