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

intelsdi-x/snap-plugin-collector-snmp: collector/collector.go; 94 LoC

Open
#12,522 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 [intelsdi-x/snap-plugin-collector-snmp](https://www.github.com/intelsdi-x/snap-plugin-collector-snmp) at [collector/collector.go](https://github.com/intelsdi-x/snap-plugin-collector-snmp/blob/1db81c0d333d2292b1ea9da8a065bd16f54b1c38/collector/collector.go#L198-L291)

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 metric used in defer or goroutine at line 266

[Click here to see the code in its original context.](https://github.com/intelsdi-x/snap-plugin-collector-snmp/blob/1db81c0d333d2292b1ea9da8a065bd16f54b1c38/collector/collector.go#L198-L291)

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

```go
for _, metric := range metrics {

//get metrics to collect
metricsConfigs, err := getMetricsToCollect(metric.Namespace.String(), p.metricsConfigs)
if err != nil {
return nil, err
}

wgCollectedMetrics.Add(len(metricsConfigs))

for _, cfg := range metricsConfigs {

go func(cfg configReader.Metric) {

defer wgCollectedMetrics.Done()

conn.mtx.Lock()

//get value of metric/metrics
results, err := snmp_.readElements(conn.handler, cfg.Oid, cfg.Mode)
if err != nil {
log.Warn(err)
conn.mtx.Unlock()
return
}

//get dynamic elements of namespace parts
err = getDynamicNamespaceElements(conn.handler, results, &cfg)
if err != nil {
conn.mtx.Unlock()
return
}

conn.lastUsed = time.Now()
conn.mtx.Unlock()

for i, result := range results {

//build namespace for metric
namespace := plugin.NewNamespace(Vendor, PluginName)
offset := len(namespace)
for j, ns := range cfg.Namespace {
if ns.Source == configReader.NsSourceString {
namespace = namespace.AddStaticElements(ns.String)
} else {
namespace = namespace.AddDynamicElement(ns.Name, ns.Description)
namespace[j+offset].Value = ns.Values[i]
}
}

//convert metric types
val, err := convertSnmpDataToMetric(result.Variable.String(), result.Variable.Type())
if err != nil {
continue
}

//modify numeric metric - use scale and shift parameters
data := modifyNumericMetric(val, cfg.Scale, cfg.Shift)

//creating metric
mt := plugin.Metric{
Namespace: namespace,
Data: data,
Timestamp: time.Now(),
Tags: map[string]string{
tagSnmpAgentName: agentConfig.Name,
tagSnmpAgentAddress: agentConfig.Address,
tagOid: result.Oid.String()},
Unit: metric.Unit,
Description: metric.Description,
}

//adding metric to list of metrics
mtxMetrics.Lock()

//filter specific instance
nsPattern := strings.Replace(metric.Namespace.String(), "*", ".*", -1)
matched, err := regexp.MatchString(nsPattern, mt.Namespace.String())
if err != nil {
logFields := map[string]interface{}{"namespace": mt.Namespace.String(), "pattern": nsPattern, "match_error": err}
err := fmt.Errorf("Cannot parse namespace element for matching")
log.WithFields(logFields).Warn(err)
return
}
if matched {
mts = append(mts, mt)
}

mtxMetrics.Unlock()
}
}(cfg)
}
wgCollectedMetrics.Wait()
}

```

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: 1db81c0d333d2292b1ea9da8a065bd16f54b1c38

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.