sa9006.go raw

   1  package sa9006
   2  
   3  import (
   4  	"fmt"
   5  	"go/ast"
   6  	"go/types"
   7  
   8  	"honnef.co/go/tools/analysis/code"
   9  	"honnef.co/go/tools/analysis/lint"
  10  	"honnef.co/go/tools/analysis/report"
  11  	"honnef.co/go/tools/pattern"
  12  
  13  	"golang.org/x/tools/go/analysis"
  14  	"golang.org/x/tools/go/analysis/passes/inspect"
  15  )
  16  
  17  var SCAnalyzer = lint.InitializeAnalyzer(&lint.Analyzer{
  18  	Analyzer: &analysis.Analyzer{
  19  		Name:     "SA9006",
  20  		Run:      run,
  21  		Requires: []*analysis.Analyzer{inspect.Analyzer},
  22  	},
  23  	Doc: &lint.RawDocumentation{
  24  		Title: `Dubious bit shifting of a fixed size integer value`,
  25  		Text: `Bit shifting a value past its size will always clear the value.
  26  
  27  For instance:
  28  
  29      v := int8(42)
  30      v >>= 8
  31  
  32  will always result in 0.
  33  
  34  This check flags bit shifting operations on fixed size integer values only.
  35  That is, int, uint and uintptr are never flagged to avoid potential false
  36  positives in somewhat exotic but valid bit twiddling tricks:
  37  
  38      // Clear any value above 32 bits if integers are more than 32 bits.
  39      func f(i int) int {
  40          v := i >> 32
  41          v = v << 32
  42          return i-v
  43      }`,
  44  		Since:    "2020.2",
  45  		Severity: lint.SeverityWarning,
  46  		// Technically this should be MergeIfAll, because the type of
  47  		// v might be different for different build tags. Practically,
  48  		// don't write code that depends on that.
  49  		MergeIf: lint.MergeIfAny,
  50  	},
  51  })
  52  
  53  var Analyzer = SCAnalyzer.Analyzer
  54  var (
  55  	checkFixedLengthTypeShiftQ = pattern.MustParse(`
  56  		(Or
  57  			(AssignStmt _ (Or ">>=" "<<=") _)
  58  			(BinaryExpr _ (Or ">>" "<<") _))
  59  	`)
  60  )
  61  
  62  func run(pass *analysis.Pass) (interface{}, error) {
  63  	isDubiousShift := func(x, y ast.Expr) (int64, int64, bool) {
  64  		typ, ok := pass.TypesInfo.TypeOf(x).Underlying().(*types.Basic)
  65  		if !ok {
  66  			return 0, 0, false
  67  		}
  68  		switch typ.Kind() {
  69  		case types.Int8, types.Int16, types.Int32, types.Int64,
  70  			types.Uint8, types.Uint16, types.Uint32, types.Uint64:
  71  			// We're only interested in fixed–size types.
  72  		default:
  73  			return 0, 0, false
  74  		}
  75  
  76  		const bitsInByte = 8
  77  		typeBits := pass.TypesSizes.Sizeof(typ) * bitsInByte
  78  
  79  		shiftLength, ok := code.ExprToInt(pass, y)
  80  		if !ok {
  81  			return 0, 0, false
  82  		}
  83  
  84  		return typeBits, shiftLength, shiftLength >= typeBits
  85  	}
  86  
  87  	fn := func(node ast.Node) {
  88  		if _, ok := code.Match(pass, checkFixedLengthTypeShiftQ, node); !ok {
  89  			return
  90  		}
  91  
  92  		switch e := node.(type) {
  93  		case *ast.AssignStmt:
  94  			if size, shift, yes := isDubiousShift(e.Lhs[0], e.Rhs[0]); yes {
  95  				report.Report(pass, e, fmt.Sprintf("shifting %d-bit value by %d bits will always clear it", size, shift))
  96  			}
  97  		case *ast.BinaryExpr:
  98  			if size, shift, yes := isDubiousShift(e.X, e.Y); yes {
  99  				report.Report(pass, e, fmt.Sprintf("shifting %d-bit value by %d bits will always clear it", size, shift))
 100  			}
 101  		}
 102  	}
 103  	code.Preorder(pass, fn, (*ast.AssignStmt)(nil), (*ast.BinaryExpr)(nil))
 104  
 105  	return nil, nil
 106  }
 107