modernc.org/gc@v1.0.1-0.20240304020402-f0dba7c97c2b/testdata/errchk/test/fixedbugs/issue21655.go (about)

     1  // compile
     2  
     3  // Copyright 2017 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  // Make sure assembly offsets don't get too large.
     8  
     9  // To trigger issue21655, the index offset needs to be small
    10  // enough to fit into an int32 (to get rewritten to an ADDQconst)
    11  // but large enough to overflow an int32 after multiplying by the stride.
    12  
    13  package main
    14  
    15  func f1(a []int64, i int64) int64 {
    16  	return a[i+1<<30]
    17  }
    18  func f2(a []int32, i int64) int32 {
    19  	return a[i+1<<30]
    20  }
    21  func f3(a []int16, i int64) int16 {
    22  	return a[i+1<<30]
    23  }
    24  func f4(a []int8, i int64) int8 {
    25  	return a[i+1<<31]
    26  }
    27  func f5(a []float64, i int64) float64 {
    28  	return a[i+1<<30]
    29  }
    30  func f6(a []float32, i int64) float32 {
    31  	return a[i+1<<30]
    32  }
    33  
    34  // Note: Before the fix for issue 21655, f{1,2,5,6} made
    35  // the compiler crash. f3 silently generated the wrong
    36  // code, using an offset of -1<<31 instead of 1<<31.
    37  // (This is due to the assembler accepting offsets
    38  // like 0x80000000 and silently using them as
    39  // signed 32 bit offsets.)
    40  // f4 was ok, but testing it can't hurt.
    41  
    42  func f7(ss []*string, i int) string {
    43  	const offset = 3 << 29 // 3<<29 * 4 = 3<<31 = 1<<31 mod 1<<32.
    44  	if i > offset {
    45  		return *ss[i-offset]
    46  	}
    47  	return ""
    48  }
    49  func f8(ss []*string, i int) string {
    50  	const offset = 3<<29 + 10
    51  	if i > offset {
    52  		return *ss[i-offset]
    53  	}
    54  	return ""
    55  }
    56  func f9(ss []*string, i int) string {
    57  	const offset = 3<<29 - 10
    58  	if i > offset {
    59  		return *ss[i-offset]
    60  	}
    61  	return ""
    62  }