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

tenta-browser/tenta-dns: runtime/cache.go; 69 LoC

Open
#9,430 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 [tenta-browser/tenta-dns](https://www.github.com/tenta-browser/tenta-dns) at [runtime/cache.go](https://github.com/tenta-browser/tenta-dns/blob/8a8ab0bbeb86f54d3a26606118d06ca03e49dfdd/runtime/cache.go#L487-L555)

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 k used in defer or goroutine at line 500

[Click here to see the code in its original context.](https://github.com/tenta-browser/tenta-dns/blob/8a8ab0bbeb86f54d3a26606118d06ca03e49dfdd/runtime/cache.go#L487-L555)

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

```go
for k, v := range interm {
// switch cahceElemType := v.(type) {
// case *itemCache:
// // fmt.Printf("We have regular [%s] -- [%v]\n", k, v)
// case *responseCache:
// // fmt.Printf("We have DNSSEC [%s] -- [%s]\n", k, cahceElemType.Question[0].String())
// }

/// if item is queried before rounded eviction time
if v.timeCreated().Add(v.validity()).Before(time.Now()) {
// d.lg.Debugf("Deleting record, because [%v] + [%v] > [%v]", v.timeCreated(), v.validity(), time.Now())
defer func() {
dom.m.Lock()
delete(interm, k)
dom.m.Unlock()
}()
continue
} else { /// if opaque cache item has valid TTL
// d.lg.Debugf("Item is within validity period. Returning as requested, or as possible.")
if dnssec && v.isDNSSECStore() { /// if we need dnssec and we have a dnssec response, we return *the* response (only one of those per RRtype)
// d.lg.Debugf("Returning DNSSEC cache -- [%v]", v.(*responseCache).Msg.Question[0])
// defer func(m *sync.RWMutex) {
// m.Lock()
// v.adjustValidity(int64(-time.Now().Sub(v.timeCreated()) / time.Second))
// m.Unlock()
// }(dom.m)
src := v.(*responseCache).Msg
retResp := cloneResponse(src)
for hldIndex, holder := range [][]dns.RR{src.Answer, src.Ns, cleanAdditionalSection(src.Extra)} {
for _, rr := range holder {
if rr != nil {
rr.Header().Ttl = rr.Header().Ttl - uint32(time.Now().Sub(v.timeCreated())/time.Second)
} else if hldIndex == 0 { /// if a record got stale from the answer section, remove this entry from cache
defer func() {
dom.m.Lock()
delete(interm, k)
dom.m.Unlock()
}()
continue
}
}
}
dom.m.RUnlock()
return retResp, nil
} else if !v.isDNSSECStore() {
// fmt.Printf("Returning regular cache item -- [%v] with extra [%v]\n", v.(*itemCache).RR, v.(*itemCache).val)
// defer func(m *sync.RWMutex) {
// m.Lock()
// v.adjustValidity(int64(-time.Now().Sub(v.timeCreated()) / time.Second))
// m.Unlock()
// }(dom.m)
retRR := dns.Copy(v.(*itemCache).RR)
if retRR.Header().Ttl > uint32(time.Now().Sub(v.timeCreated())/time.Second) {
retRR.Header().Ttl = retRR.Header().Ttl - uint32(time.Now().Sub(v.timeCreated())/time.Second)
} else {
defer func() {
dom.m.Lock()
delete(interm, k)
dom.m.Unlock()
}()
continue
}
retRegular = append(retRegular, retRR)
if extra == nil && v.(*itemCache).val != nil {
extra = v.(*itemCache).val
}
}
}
}

```

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: 8a8ab0bbeb86f54d3a26606118d06ca03e49dfdd

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.