Source file test/fixedbugs/issue41780.go

     1  // run
     2  
     3  // Copyright 2020 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  // Checks that conversion of CMP(x,-y) -> CMN(x,y) is only applied in correct context.
     8  
     9  package main
    10  
    11  type decimal struct {
    12  	d  [8]byte // digits, big-endian representation
    13  	dp int     // decimal point
    14  }
    15  
    16  var powtab = []int{1, 3, 6, 9, 13, 16, 19, 23, 26}
    17  
    18  //go:noinline
    19  func foo(d *decimal) int {
    20  	exp := int(d.d[1])
    21  	if d.dp < 0 || d.dp == 0 && d.d[0] < '5' {
    22  		var n int
    23  		if -d.dp >= len(powtab) {
    24  			n = 27
    25  		} else {
    26  			n = powtab[-d.dp] // incorrect CMP -> CMN substitution causes indexing panic.
    27  		}
    28  		exp += n
    29  	}
    30  	return exp
    31  }
    32  
    33  func main() {
    34  	var d decimal
    35  	d.d[0] = '1'
    36  	if foo(&d) != 1 {
    37  		println("FAILURE (though not the one this test was written to catch)")
    38  	}
    39  }
    40  

View as plain text