Source file test/fixedbugs/issue19275.go

     1  // run
     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  package main
     8  
     9  import (
    10  	"fmt"
    11  )
    12  
    13  type PI struct {
    14  	Enabled bool
    15  }
    16  
    17  type SI struct {
    18  	M map[string]*PI
    19  }
    20  
    21  //go:noinline
    22  func (s *SI) test(name string) (*int, error) {
    23  	n := new(int)
    24  	*n = 99
    25  	if err := addUpdate(n, s.M[name].Enabled, "enabled"); err != nil { // this was miscompiled
    26  		return nil, fmt.Errorf(" error adding update for enable flag %t : %s",
    27  			s.M[name].Enabled, err)
    28  	}
    29  	return n, nil
    30  }
    31  
    32  //go:noinline
    33  func addUpdate(n *int, in interface{}, s ...string) error {
    34  	if *n != 99 {
    35  		println("FAIL, *n should be 99, not", *n)
    36  	}
    37  	return nil
    38  }
    39  
    40  func main1() {
    41  	s := &SI{make(map[string]*PI)}
    42  	s.M["dog"] = &PI{}
    43  	s.test("dog")
    44  }
    45  
    46  //go:noinline
    47  func g(b *byte, i interface{}) error {
    48  	if *b != 17 {
    49  		println("FAIL, *b should be 17, not", *b)
    50  	}
    51  	return nil
    52  }
    53  
    54  //go:noinline
    55  func f(x *byte, m map[string]*bool) {
    56  	if err := g(x, *m["hello"]); err != nil { // this was miscompiled
    57  		return
    58  	}
    59  }
    60  
    61  func main2() {
    62  	m := make(map[string]*bool)
    63  	x := false
    64  	m["hello"] = &x
    65  	b := byte(17)
    66  	f(&b, m)
    67  }
    68  
    69  func main() {
    70  	main2()
    71  	main1()
    72  }
    73  

View as plain text