github-vet / github-vet/rangeloop-pointer-findings
sah4ez/xsync: pkg/query/query.go; 160 LoC
- Dominant language
- No language data
- Stars
- 0
- Forks
- 0
- PR merge metrics
- PR metrics pending
Description
Found a possible issue in [sah4ez/xsync](https://www.github.com/sah4ez/xsync) at [pkg/query/query.go](https://github.com/sah4ez/xsync/blob/986046da3d81cc9939e4b34630cfa02b824853d3/pkg/query/query.go#L31-L190)
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 schema used in defer or goroutine at line 42
[Click here to see the code in its original context.](https://github.com/sah4ez/xsync/blob/986046da3d81cc9939e4b34630cfa02b824853d3/pkg/query/query.go#L31-L190)
Click here to show the 160 line(s) of Go which triggered the analyzer.
```go
for schema, tables := range q.Tables {
for _, t := range tables {
table := t
go func() {
interval := 1 * time.Second
if table.Interval > time.Duration(0) {
interval = table.Interval
}
settings := builder.Select().
Column("value").
From(SettingsTable).
Where("key_id='" + schema + "." + t.Table + "'")
settingsStr, err := settings.ToSql()
if err != nil {
q.logger.Error(
"build query settings",
zap.String("err", err.Error()))
return
}
q.tl.Lock()
v, err := q.Target.Execute(settingsStr)
q.tl.Unlock()
var vv uint64
if v.ColumnNumber() != 1 || v.RowNumber() != 1 {
q.logger.Warn(
"build query settings",
zap.String("err", fmt.Sprintf("invalid cound column %d or rows %d for settings %s",
v.ColumnNumber(), v.RowNumber(), schema+"."+t.Table)))
} else {
vv, err = v.GetUint(0, 0)
if err != nil {
q.logger.Error(
"build query settings",
zap.String("err", err.Error()))
return
}
}
for {
select {
case <-time.After(interval):
task := func() error {
t := table
q.logger.Info(
"start sync",
zap.String("shcema", schema),
zap.String("table", t.Table))
selectStr := builder.Select().
Column("*").
From(schema+"."+t.Table).
Where(t.FieldID+">"+fmt.Sprintf("%d", vv)).
OrderBy(builder.Desc, t.FieldID)
if t.Batch != "0" {
selectStr = selectStr.Limit(t.Batch)
}
str, err := selectStr.ToSql()
if err != nil {
return fmt.Errorf("build query error: %s", err.Error())
}
q.sl.Lock()
v, err := q.Source.Execute(str)
q.sl.Unlock()
if err != nil {
return fmt.Errorf("source query: %s has error: %s", str, err.Error())
}
if v != nil && v.Resultset != nil {
maxId, _ := v.GetUint(0, 0)
insert := builder.Insert().Table(schema + "." + t.Table)
fields := make([]string, len(v.Resultset.FieldNames))
for field, i := range v.Resultset.FieldNames {
fields[i] = field
}
insert = insert.Column(fields...)
if len(v.Resultset.RowDatas) == 0 {
q.logger.Warn("row datas is empty",
zap.String("query", str),
)
return nil
}
for _, vvv := range v.Resultset.RowDatas {
vv, err := vvv.ParseText(v.Resultset.Fields)
if err != nil {
return fmt.Errorf("parse query: %s has error: %s", str, err.Error())
}
var vals []string
for _, val := range vv {
valUint8, ok := val.([]uint8)
if ok {
valUint8str := B2S(valUint8)
intValue := ""
if _, err := strconv.Atoi(valUint8str); err != nil {
intValue = "'" + valUint8str + "'"
} else {
intValue = valUint8str
}
vals = append(vals, intValue)
}
}
insert = insert.Value(vals...)
}
var onDuplicate []string
for _, field := range fields {
if field != t.FieldID {
onDuplicate = append(onDuplicate, field)
}
}
insert = insert.OnDuplicateKeyUpdate(onDuplicate...)
insertStr, err := insert.ToSql()
if err != nil {
return fmt.Errorf("build insert query has error: %s", err.Error())
}
q.tl.Lock()
v, err = q.Target.Execute(insertStr)
q.tl.Unlock()
if err != nil {
return fmt.Errorf("insert query: %s has error: %s", insertStr, err.Error())
}
insertSetting := builder.Insert().
Table(SettingsTable).
Column("key_id", "value").
Value("'"+schema+"."+t.Table+"'", fmt.Sprintf("'%d'", maxId)).
OnDuplicateKeyUpdate("value")
insertStr, err = insertSetting.ToSql()
if err != nil {
return fmt.Errorf("build insert setting query has error: %s", err.Error())
}
q.tl.Lock()
_, err = q.Target.Execute(insertStr)
q.tl.Unlock()
if err != nil {
return fmt.Errorf("build insert setting query has error: %s", err.Error())
}
}
return nil
}
q.Pool.Schedule(task)
}
}
}()
}
}
```
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: 986046da3d81cc9939e4b34630cfa02b824853d3
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.