Skip to content

Commit

Permalink
*: Enable gosec for implicit memory aliasing in for loop (#28996)
Browse files Browse the repository at this point in the history
  • Loading branch information
janisz committed Nov 19, 2021
1 parent 2e94e58 commit 7aa86cc
Show file tree
Hide file tree
Showing 16 changed files with 49 additions and 45 deletions.
3 changes: 0 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ linters-settings:
checks: ["S1002","S1004","S1007","S1009","S1010","S1012","S1019","S1020","S1021","S1024","S1030","SA2*","SA3*","SA4009","SA5*","SA6000","SA6001","SA6005", "-SA2002"]
stylecheck:
checks: ["-ST1003"]
gosec:
excludes:
- G601
issues:
exclude-rules:
- path: _test\.go
Expand Down
9 changes: 6 additions & 3 deletions bindinfo/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ func (br *BindRecord) HasUsingBinding() bool {

// FindBinding find bindings in BindRecord.
func (br *BindRecord) FindBinding(hint string) *Binding {
for _, binding := range br.Bindings {
for i := range br.Bindings {
binding := br.Bindings[i]
if binding.ID == hint {
return &binding
}
Expand Down Expand Up @@ -160,7 +161,8 @@ func merge(lBindRecord, rBindRecord *BindRecord) *BindRecord {
return lBindRecord
}
result := lBindRecord.shallowCopy()
for _, rbind := range rBindRecord.Bindings {
for i := range rBindRecord.Bindings {
rbind := rBindRecord.Bindings[i]
found := false
for j, lbind := range lBindRecord.Bindings {
if lbind.isSame(&rbind) {
Expand All @@ -184,7 +186,8 @@ func (br *BindRecord) remove(deleted *BindRecord) *BindRecord {
return &BindRecord{OriginalSQL: br.OriginalSQL, Db: br.Db}
}
result := br.shallowCopy()
for _, deletedBind := range deleted.Bindings {
for j := range deleted.Bindings {
deletedBind := deleted.Bindings[j]
for i, bind := range result.Bindings {
if bind.isSame(&deletedBind) {
result.Bindings = append(result.Bindings[:i], result.Bindings[i+1:]...)
Expand Down
3 changes: 2 additions & 1 deletion ddl/label/attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ func (labels *Labels) Restore() string {

// Add will add a new attribute, with validation of all attributes.
func (labels *Labels) Add(label Label) error {
for _, l := range *labels {
for i := range *labels {
l := (*labels)[i]
res := label.CompatibleWith(&l)
if res == AttributesCompatible {
continue
Expand Down
3 changes: 2 additions & 1 deletion ddl/placement/constraints.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ func (constraints *Constraints) Restore() (string, error) {
func (constraints *Constraints) Add(label Constraint) error {
pass := true

for _, cnst := range *constraints {
for i := range *constraints {
cnst := (*constraints)[i]
res := label.CompatibleWith(&cnst)
if res == ConstraintCompatible {
continue
Expand Down
4 changes: 2 additions & 2 deletions executor/coprocessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ func (h *CoprocessorDAGHandler) buildResponseAndSendToStream(chk *chunk.Chunk, t
return stream.Send(h.buildErrorResponse(err))
}

for _, c := range chunks {
resp := h.buildStreamResponse(&c)
for i := range chunks {
resp := h.buildStreamResponse(&chunks[i])
if err = stream.Send(resp); err != nil {
return err
}
Expand Down
8 changes: 4 additions & 4 deletions executor/distsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -1164,19 +1164,19 @@ func (w *tableWorker) compareData(ctx context.Context, task *lookupTableTask, ta
}
tablecodec.TruncateIndexValues(tblInfo, w.idxLookup.index, vals)
sctx := w.idxLookup.ctx.GetSessionVars().StmtCtx
for i, val := range vals {
for i := range vals {
col := w.idxTblCols[i]
tp := &col.FieldType
idxVal := idxRow.GetDatum(i, tp)
tablecodec.TruncateIndexValue(&idxVal, w.idxLookup.index.Columns[i], col.ColumnInfo)
cmpRes, err := idxVal.CompareDatum(sctx, &val)
cmpRes, err := idxVal.CompareDatum(sctx, &vals[i])
if err != nil {
return ErrDataInConsistentMisMatchIndex.GenWithStackByArgs(col.Name,
handle, idxRow.GetDatum(i, tp), val, err)
handle, idxRow.GetDatum(i, tp), vals[i], err)
}
if cmpRes != 0 {
return ErrDataInConsistentMisMatchIndex.GenWithStackByArgs(col.Name,
handle, idxRow.GetDatum(i, tp), val, err)
handle, idxRow.GetDatum(i, tp), vals[i], err)
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions executor/infoschema_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -1453,13 +1453,13 @@ func (e *memtableRetriever) setDataForTiKVRegionStatus(ctx sessionctx.Context) e
}
allSchemas := ctx.GetInfoSchema().(infoschema.InfoSchema).AllSchemas()
tableInfos := tikvHelper.GetRegionsTableInfo(regionsInfo, allSchemas)
for _, region := range regionsInfo.Regions {
tableList := tableInfos[region.ID]
for i := range regionsInfo.Regions {
tableList := tableInfos[regionsInfo.Regions[i].ID]
if len(tableList) == 0 {
e.setNewTiKVRegionStatusCol(&region, nil)
e.setNewTiKVRegionStatusCol(&regionsInfo.Regions[i], nil)
}
for _, table := range tableList {
e.setNewTiKVRegionStatusCol(&region, &table)
for j := range tableList {
e.setNewTiKVRegionStatusCol(&regionsInfo.Regions[i], &tableList[j])
}
}
return nil
Expand Down Expand Up @@ -1508,8 +1508,8 @@ func (e *memtableRetriever) setDataForTikVRegionPeers(ctx sessionctx.Context) er
if err != nil {
return err
}
for _, region := range regionsInfo.Regions {
e.setNewTiKVRegionPeersCols(&region)
for i := range regionsInfo.Regions {
e.setNewTiKVRegionPeersCols(&regionsInfo.Regions[i])
}
return nil
}
Expand Down
7 changes: 4 additions & 3 deletions executor/show_placement.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ func (e *ShowExec) fetchShowPlacementForPartition(ctx context.Context) (err erro
}

var partition *model.PartitionDefinition
for _, par := range tblInfo.Partition.Definitions {
par := par
for i := range tblInfo.Partition.Definitions {
par := tblInfo.Partition.Definitions[i]
if par.Name.L == e.Partition.L {
partition = &par
break
Expand Down Expand Up @@ -330,7 +330,8 @@ func (e *ShowExec) fetchAllTablePlacements(ctx context.Context, scheduleState ma
}

if tblInfo.Partition != nil {
for _, partition := range tblInfo.Partition.Definitions {
for i := range tblInfo.Partition.Definitions {
partition := tblInfo.Partition.Definitions[i]
partitionPlacement, err := e.getPartitionPlacement(tblPlacement, &partition)
if err != nil {
return err
Expand Down
16 changes: 6 additions & 10 deletions executor/show_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,8 @@ func (e *ShowExec) appendTableForStatsHealthy(dbName, tblName, partitionName str
func (e *ShowExec) fetchShowAnalyzeStatus() {
rows := dataForAnalyzeStatusHelper(e.baseExecutor.ctx)
for _, row := range rows {
for i, val := range row {
e.result.AppendDatum(i, &val)
for i := range row {
e.result.AppendDatum(i, &row[i])
}
}
}
Expand Down Expand Up @@ -498,14 +498,10 @@ func (e *ShowExec) fetchShowColumnStatsUsage() error {
pi := tbl.GetPartitionInfo()
if pi == nil || e.ctx.GetSessionVars().UseDynamicPartitionPrune() {
appendTableForColumnStatsUsage(db.Name.O, tbl, pi != nil, nil)
if pi != nil {
for _, def := range pi.Definitions {
appendTableForColumnStatsUsage(db.Name.O, tbl, false, &def)
}
}
} else {
for _, def := range pi.Definitions {
appendTableForColumnStatsUsage(db.Name.O, tbl, false, &def)
}
if pi != nil {
for i := range pi.Definitions {
appendTableForColumnStatsUsage(db.Name.O, tbl, false, &pi.Definitions[i])
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion planner/core/expression_rewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2059,7 +2059,8 @@ func decodeRecordKey(key []byte, tableID int64, tbl table.Table, loc *time.Locat
ret := make(map[string]interface{})
ret["table_id"] = tableID
handleRet := make(map[string]interface{})
for colID, dt := range datumMap {
for colID := range datumMap {
dt := datumMap[colID]
dtStr, err := datumToJSONObject(&dt)
if err != nil {
return "", errors.Trace(err)
Expand Down
4 changes: 2 additions & 2 deletions planner/core/find_best_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -1847,8 +1847,8 @@ func (ds *DataSource) convertToPointGet(prop *property.PhysicalProperty, candida
var partitionInfo *model.PartitionDefinition
if ds.isPartition {
if pi := ds.tableInfo.GetPartitionInfo(); pi != nil {
for _, def := range pi.Definitions {
def := def
for i := range pi.Definitions {
def := pi.Definitions[i]
if def.ID == ds.physicalTableID {
partitionInfo = &def
break
Expand Down
3 changes: 2 additions & 1 deletion privilege/privileges/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -1257,7 +1257,8 @@ func (p *MySQLPrivilege) showGrants(user, host string, roles []*auth.RoleIdentit
// A map of "DB.Table" => Priv(col1, col2 ...)
sortFromIdx = len(gs)
columnPrivTable := make(map[string]privOnColumns)
for _, record := range p.ColumnsPriv {
for i := range p.ColumnsPriv {
record := p.ColumnsPriv[i]
if !collectColumnGrant(&record, user, host, columnPrivTable) {
for _, r := range allRoles {
collectColumnGrant(&record, r.Username, r.Hostname, columnPrivTable)
Expand Down
3 changes: 2 additions & 1 deletion statistics/handle/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ func (m indexUsageMap) update(tableID int64, indexID int64, value *IndexUsageInf
}

func (m indexUsageMap) merge(destMap indexUsageMap) {
for id, item := range destMap {
for id := range destMap {
item := destMap[id]
m.updateByKey(id, &item)
}
}
Expand Down
3 changes: 2 additions & 1 deletion store/copr/key_ranges.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ func (r *KeyRanges) Do(f func(ran *kv.KeyRange)) {
if r.first != nil {
f(r.first)
}
for _, ran := range r.mid {
for i := range r.mid {
ran := r.mid[i]
f(&ran)
}
if r.last != nil {
Expand Down
8 changes: 4 additions & 4 deletions store/mockstore/unistore/cophandler/mpp_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ func (e *exchSenderExec) next() (*chunk.Chunk, error) {
}
return nil, nil
}
for _, tipbChunk := range tipbChunks {
tunnel.DataCh <- &tipbChunk
for j := range tipbChunks {
tunnel.DataCh <- &tipbChunks[j]
}
}
}
Expand All @@ -194,8 +194,8 @@ func (e *exchSenderExec) next() (*chunk.Chunk, error) {
}
return nil, nil
}
for _, tipbChunk := range tipbChunks {
tunnel.DataCh <- &tipbChunk
for i := range tipbChunks {
tunnel.DataCh <- &tipbChunks[i]
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion util/codec/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,8 @@ func HashChunkSelected(sc *stmtctx.StatementContext, h []hash.Hash64, chk *chunk
}
case mysql.TypeDouble:
f64s := column.Float64s()
for i, f := range f64s {
for i := range f64s {
f := f64s[i]
if sel != nil && !sel[i] {
continue
}
Expand Down

0 comments on commit 7aa86cc

Please sign in to comment.