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

nwihardjo/SpaghettiSearch: indexer/indexer.go; 137 LoC

Open
#14,817 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 [nwihardjo/SpaghettiSearch](https://www.github.com/nwihardjo/SpaghettiSearch) at [indexer/indexer.go](https://github.com/nwihardjo/SpaghettiSearch/blob/6bdb64972293a21ac2ed377b751d64cc642dd968/indexer/indexer.go#L163-L299)

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 kid used in defer or goroutine at line 202

[Click here to see the code in its original context.](https://github.com/nwihardjo/SpaghettiSearch/blob/6bdb64972293a21ac2ed377b751d64cc642dd968/indexer/indexer.go#L163-L299)

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

```go
for idx, kid := range kids {
bw_anchor := inverted[0].BatchWrite_init(ctx)
defer bw_anchor.Cancel(ctx)
bw_anchor_frw := forward[0].BatchWrite_init(ctx)
defer bw_anchor_frw.Cancel(ctx)

// Get DocInfo corresponding to the child,
// make one if not present (for the sake of getting the url of not-yet-visited child)
docInfoC, err := forward[1].Get(ctx, kid)
if err == badger.ErrKeyNotFound {
tempP := make(map[string][]string)
if cleanFancy[kid] == nil {
tempP[docHashString] = []string{}
} else {
tempP[docHashString] = cleanFancy[kid]
}
docInfoC_ := database.DocInfo{*kidUrls[idx], nil, time.Time{}, 0, nil, tempP, nil}

// Set docHash of child -> docInfo of child using batch writer
if err = bw_child.BatchSet(ctx, kid, docInfoC_); err != nil {
panic(err)
}

tttt := make(map[string]uint32)
babi := make(map[string][]float32)
for _, w := range cleanFancy[kid] {
tttt[w] += 1
babi[w] = append(babi[w], -100)
}
maxFreq := getMaxFreq(fancyInfo[kid].Freq)
var wg1 sync.WaitGroup
for wrd, _ := range tttt {
wg1.Add(1)
go func(w string) {
defer wg1.Done()
wHash := md5.Sum([]byte(w))
wHashString := hex.EncodeToString(wHash[:])
invKeyVals := make(map[string][]float32)
normTF := float32(float32(tttt[w]) / float32(maxFreq))
invKeyVals[kid] = append([]float32{normTF}, babi[w]...)
// append the added entry (docHash and pos) to inverted file
// value has type of map[DocHash][]uint32 (docHash -> list of position)
value, err := inverted[0].Get(ctx, wHashString)
if err == badger.ErrKeyNotFound {
// there's no entry on the inverted table for the corresponding wordHash
if err = bw_anchor.BatchSet(ctx, wHashString, invKeyVals); err != nil {
panic(err)
}
if err = bw_anchor_frw.BatchSet(ctx, wHashString, w); err != nil {
panic(err)
}
} else if err != nil {
panic(err)
} else {
// append new docHash entry to the existing one
value.(map[string][]float32)[kid] = invKeyVals[kid]

// load new appended value of inverted table according to the wordHash
if err = bw_anchor.BatchSet(ctx, wHashString, value); err != nil {
panic(err)
}
}
}(wrd)
}
wg1.Wait()
} else if err != nil {
panic(err)
} else {
docInfoC_ := docInfoC.(database.DocInfo)
if docInfoC_.Parents == nil {
docInfoC_.Parents = make(map[string][]string)
}
docInfoC_.Parents[docHashString] = cleanFancy[kid]
// Set docHash of child -> docInfo of child using batch writer
if err = bw_child.BatchSet(ctx, kid, docInfoC_); err != nil {
panic(err)
}
tttt := make(map[string]uint32)
babi := make(map[string][]float32)
for _, w := range cleanFancy[kid] {
tttt[w] += 1
babi[w] = append(babi[w], -100)
}
tempCleanFancyUnique := tttt
for i, w := range docInfoC_.Page_title {
tttt[w] += 1
babi[w] = append(babi[w], float32(i))
}
maxFreq := uint32(0)
for _, v := range tttt {
if v > maxFreq {
maxFreq = v
}
}
var wg1 sync.WaitGroup
for wrd, _ := range tempCleanFancyUnique {
wg1.Add(1)
go func(w string) {
defer wg1.Done()
wHash := md5.Sum([]byte(w))
wHashString := hex.EncodeToString(wHash[:])
invKeyVals := make(map[string][]float32)
normTF := float32(float32(tttt[w]) / float32(maxFreq))
invKeyVals[kid] = append([]float32{normTF}, babi[w]...)
// append the added entry (docHash and pos) to inverted file
// value has type of map[DocHash][]uint32 (docHash -> list of position)
value, err := inverted[0].Get(ctx, wHashString)
if err == badger.ErrKeyNotFound {
// there's no entry on the inverted table for the corresponding wordHash
if err = bw_anchor.BatchSet(ctx, wHashString, invKeyVals); err != nil {
panic(err)
}
if err = bw_anchor_frw.BatchSet(ctx, wHashString, w); err != nil {
panic(err)
}
} else if err != nil {
panic(err)
} else {
// append new docHash entry to the existing one
value.(map[string][]float32)[kid] = invKeyVals[kid]

// load new appended value of inverted table according to the wordHash
if err = bw_anchor.BatchSet(ctx, wHashString, value); err != nil {
panic(err)
}
}
}(wrd)
}
wg1.Wait()
}
if err := bw_anchor.Flush(ctx); err != nil {
panic(err)
}
if err := bw_anchor_frw.Flush(ctx); err != nil {
panic(err)
}
}

```

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: 6bdb64972293a21ac2ed377b751d64cc642dd968

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.