github.com/geraldss/go/src@v0.0.0-20210511222824-ac7d0ebfc235/runtime/checkptr_test.go (about) 1 // Copyright 2020 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package runtime_test 6 7 import ( 8 "internal/testenv" 9 "os/exec" 10 "strings" 11 "testing" 12 ) 13 14 func TestCheckPtr(t *testing.T) { 15 t.Parallel() 16 testenv.MustHaveGoRun(t) 17 18 exe, err := buildTestProg(t, "testprog", "-gcflags=all=-d=checkptr=1") 19 if err != nil { 20 t.Fatal(err) 21 } 22 23 testCases := []struct { 24 cmd string 25 want string 26 }{ 27 {"CheckPtrAlignmentPtr", "fatal error: checkptr: misaligned pointer conversion\n"}, 28 {"CheckPtrAlignmentNoPtr", ""}, 29 {"CheckPtrArithmetic", "fatal error: checkptr: pointer arithmetic result points to invalid allocation\n"}, 30 {"CheckPtrArithmetic2", "fatal error: checkptr: pointer arithmetic result points to invalid allocation\n"}, 31 {"CheckPtrSize", "fatal error: checkptr: converted pointer straddles multiple allocations\n"}, 32 {"CheckPtrSmall", "fatal error: checkptr: pointer arithmetic computed bad pointer value\n"}, 33 } 34 35 for _, tc := range testCases { 36 tc := tc 37 t.Run(tc.cmd, func(t *testing.T) { 38 t.Parallel() 39 got, err := testenv.CleanCmdEnv(exec.Command(exe, tc.cmd)).CombinedOutput() 40 if err != nil { 41 t.Log(err) 42 } 43 if tc.want == "" { 44 if len(got) > 0 { 45 t.Errorf("output:\n%s\nwant no output", got) 46 } 47 return 48 } 49 if !strings.HasPrefix(string(got), tc.want) { 50 t.Errorf("output:\n%s\n\nwant output starting with: %s", got, tc.want) 51 } 52 }) 53 } 54 }