Source file test/fixedbugs/issue17918.go

     1  // compile
     2  
     3  // Copyright 2016 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  // Issue 17918: slice out-of-bounds in ssa/cse
     8  
     9  package dead
    10  
    11  import (
    12  	"fmt"
    13  	"time"
    14  )
    15  
    16  var (
    17  	units = []struct {
    18  		divisor time.Duration
    19  		unit    rune
    20  	}{
    21  		{1000000, 's'},
    22  		{60, 'm'},
    23  		{60, 'h'},
    24  		{24, 'd'},
    25  		{7, 'w'},
    26  	}
    27  )
    28  
    29  func foobar(d time.Duration) string {
    30  	d /= time.Microsecond
    31  	unit := 'u'
    32  
    33  	for _, f := range units {
    34  		if d%f.divisor != 0 {
    35  			break
    36  		}
    37  		d /= f.divisor
    38  		unit = f.unit
    39  	}
    40  	return fmt.Sprintf("%d%c", d, unit)
    41  }
    42  

View as plain text