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

Tencent/bk-cmdb: src/source_controller/coreservice/core/model/attribute_curd.go; 107 LoC

Open
#16,870 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 [Tencent/bk-cmdb](https://www.github.com/Tencent/bk-cmdb) at [src/source_controller/coreservice/core/model/attribute_curd.go](https://github.com/Tencent/bk-cmdb/blob/4347923164dc5bdd2ecd166e58d00c5a394b5c10/src/source_controller/coreservice/core/model/attribute_curd.go#L362-L468)

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 object used in defer or goroutine at line 428

[Click here to see the code in its original context.](https://github.com/Tencent/bk-cmdb/blob/4347923164dc5bdd2ecd166e58d00c5a394b5c10/src/source_controller/coreservice/core/model/attribute_curd.go#L362-L468)

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

```go
for object, objFields := range objectFields {
if len(objFields) == 0 {
// no fields need to be removed, skip directly.
continue
}

for _, objField := range objFields {
fields := objField.fields
existConds := make([]map[string]interface{}, len(fields))

for index, field := range fields {
existConds[index] = map[string]interface{}{
field: map[string]interface{}{
common.BKDBExists: true,
},
}
}

cond := map[string]interface{}{
common.BKDBOR: existConds,
}

if objField.bizID > 0 {
if !isBizObject(object) {
return fmt.Errorf("unsupported object %s's clean instance field operation", object)
}

if object == common.BKInnerObjIDHost {
if err := m.cleanHostAttributeField(ctx, ownerID, objField); err != nil {
return err
}
continue
}

cond[common.BKAppIDField] = objField.bizID
} else {
if isBizObject(object) {
if object == common.BKInnerObjIDHost {
ele := bizObjectFields{
bizID: 0,
fields: fields,
}
if err := m.cleanHostAttributeField(ctx, ownerID, ele); err != nil {
return err
}
continue
}
} else {
cond[common.BKObjIDField] = object
}
}

cond = util.SetQueryOwner(cond, ownerID)

collectionName := common.GetInstTableName(object)
wg.Add(1)
go func(collName string, filter types.Filter, fields []string) {
defer wg.Done()

instCount, err := mongodb.Client().Table(collName).Find(filter).Count(ctx)
if err != nil {
blog.Error("count instances with the attribute to delete failed, table: %s, cond: %v, fields: %v, err: %v", collectionName, filter, fields, err)
hitError = err
return
}

instIDField := common.GetInstIDField(object)
for start := uint64(0); start < instCount; start += pageSize {
insts := make([]map[string]interface{}, 0)
err := mongodb.Client().Table(collName).Find(filter).Start(0).Limit(pageSize).Fields(instIDField).All(ctx, &insts)
if err != nil {
blog.Error("get instance ids with the attribute to delete failed, table: %s, cond: %v, fields: %v, err: %v", collectionName, filter, fields, err)
hitError = err
return
}

if len(insts) == 0 {
return
}

instIDs := make([]int64, len(insts))
for index, inst := range insts {
instID, err := util.GetInt64ByInterface(inst[instIDField])
if err != nil {
blog.Error("get instance id failed, inst: %+v, err: %v", inst, err)
hitError = err
return
}
instIDs[index] = instID
}

instFilter := map[string]interface{}{
instIDField: map[string]interface{}{
common.BKDBIN: instIDs,
},
}

if err := mongodb.Client().Table(collName).DropColumns(ctx, instFilter, fields); err != nil {
blog.Error("delete object's attribute from instance failed, table: %s, cond: %v, fields: %v, err: %v", collectionName, instFilter, fields, err)
hitError = err
return
}
}
}(collectionName, cond, fields)

}
}

```

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: 4347923164dc5bdd2ecd166e58d00c5a394b5c10

Contributor guide

No contributing guide indexed for this repository

Research direction

Read src/source_controller/coreservice/core/model/attribute_curd.go around lines 362-468, especially the goroutine near line 428. Check whether the range variable object is safely scoped for each deferred or concurrent operation and whether the analyzer finding is valid; done means leaving a clear Bug, Mitigated, or Desirable Behavior classification based on that review.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.