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

logd-team/logd: etl_outputer.go; 65 LoC

Open
#12,841 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 [logd-team/logd](https://www.github.com/logd-team/logd) at [etl_outputer.go](https://github.com/logd-team/logd/blob/4d122b38ea263dff86790ecc356369c40178b6f0/etl_outputer.go#L114-L178)

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 b is reassigned at line 117

[Click here to see the code in its original context.](https://github.com/logd-team/logd/blob/4d122b38ea263dff86790ecc356369c40178b6f0/etl_outputer.go#L114-L178)

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

```go
for b := range e.buffer {
loglib.Info(fmt.Sprintf("pack in chan: %d", len(e.buffer)))
buf := make([]byte, 4)
bp := &b
bp.Read(buf)

l, _ := binary.Uvarint(buf)
headerLen := int(l)
//get pack header
buf = make([]byte, headerLen)
bp.Read(buf)
header := tcp_pack.ParseHeader(buf)

r, err := zlib.NewReader(bp)
if err != nil {
loglib.Error("zlib reader Error: " + err.Error())
}else{
lines, _ := strconv.Atoi(header["lines"])
done := false
if header["done"] == "1" {
done = true
}
e.ic.Add(header["ip"], header["hour"], header["id"], lines, done)

writerKey := header["ip"] + "_" + header["hour"]
fout := e.getWriter(e.writers, e.dataDir, writerKey)

buf = append(buf, '\n')
/*
//一头一尾写头信息,节省硬盘
n, err := fout.Write(buf)
if err != nil {
loglib.Info(fmt.Sprintf("write %s %d %s", writerKey, n, err.Error()))
}
*/
nn, err := io.Copy(fout, r)
if err != nil {
loglib.Warning(fmt.Sprintf("save %s_%s_%s error:%s, saved:%d", header["ip"], header["hour"], header["id"], err, nn))
}
//fout.Write(buf)
//单独存一份header便于查数
fout = e.getWriter(e.headerWriters, e.headerDir, writerKey)
n, err := fout.Write(buf)
if err != nil {
loglib.Info(fmt.Sprintf("writer header %s %d %s", writerKey, n, err.Error()))
}
//增加2分钟check一次的规则,避免done包先到,其他的包未到,则可能要等到下一小时才能check
if done || time.Now().Unix() > nextCheckTime.Unix() {
hourFinish, _ := e.ic.Check()
for ip, hours := range hourFinish {
for _, hour := range hours {
writerKey = ip + "_" + hour
loglib.Info(fmt.Sprintf("fkeychan %d", len(fkeyChan)))
fkeyChan <- writerKey
}
}
e.closeWriters(e.writers)
e.closeWriters(e.headerWriters)
e.ic.SaveStatus()
nextCheckTime = time.Now().Add(2 * time.Minute)
}

r.Close()
}
}

```

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: 4d122b38ea263dff86790ecc356369c40178b6f0

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.