Skip to content

Commit 741dad2

Browse files
hirochachachaianlancetaylor
authored andcommitted
cmd/cgo: avoid name confliction for C functions
Use more cryptic names for local variables inside C function wrappers. Fixes #23356 Change-Id: Ia6a0218f27a13be14f589b1a0facc9683d22ff56 Reviewed-on: https://go-review.googlesource.com/86495 Run-TryBot: Tobias Klauser <[email protected]> Reviewed-by: Tobias Klauser <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent b7d9e6e commit 741dad2

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

misc/cgo/test/cgo_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ func Test21897(t *testing.T) { test21897(t) }
8989
func Test22906(t *testing.T) { test22906(t) }
9090
func Test24206(t *testing.T) { test24206(t) }
9191
func Test25143(t *testing.T) { test25143(t) }
92+
func Test23356(t *testing.T) { test23356(t) }
9293

9394
func BenchmarkCgoCall(b *testing.B) { benchCgoCall(b) }
9495
func BenchmarkGoString(b *testing.B) { benchGoString(b) }

misc/cgo/test/issue23356.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2018 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 cgotest
6+
7+
// int a(void) { return 5; };
8+
// int r(void) { return 3; };
9+
import "C"
10+
import "testing"
11+
12+
func test23356(t *testing.T) {
13+
if got, want := C.a(), C.int(5); got != want {
14+
t.Errorf("C.a() == %v, expected %v", got, want)
15+
}
16+
if got, want := C.r(), C.int(3); got != want {
17+
t.Errorf("C.r() == %v, expected %v", got, want)
18+
}
19+
}

src/cmd/cgo/out.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -609,24 +609,24 @@ func (p *Package) writeOutputFunc(fgcc *os.File, n *Name) {
609609
// We're trying to write a gcc struct that matches gc's layout.
610610
// Use packed attribute to force no padding in this struct in case
611611
// gcc has different packing requirements.
612-
fmt.Fprintf(fgcc, "\t%s %v *a = v;\n", ctype, p.packedAttribute())
612+
fmt.Fprintf(fgcc, "\t%s %v *_cgo_a = v;\n", ctype, p.packedAttribute())
613613
if n.FuncType.Result != nil {
614614
// Save the stack top for use below.
615-
fmt.Fprintf(fgcc, "\tchar *stktop = _cgo_topofstack();\n")
615+
fmt.Fprintf(fgcc, "\tchar *_cgo_stktop = _cgo_topofstack();\n")
616616
}
617617
tr := n.FuncType.Result
618618
if tr != nil {
619-
fmt.Fprintf(fgcc, "\t__typeof__(a->r) r;\n")
619+
fmt.Fprintf(fgcc, "\t__typeof__(_cgo_a->r) _cgo_r;\n")
620620
}
621621
fmt.Fprintf(fgcc, "\t_cgo_tsan_acquire();\n")
622622
if n.AddError {
623623
fmt.Fprintf(fgcc, "\terrno = 0;\n")
624624
}
625625
fmt.Fprintf(fgcc, "\t")
626626
if tr != nil {
627-
fmt.Fprintf(fgcc, "r = ")
627+
fmt.Fprintf(fgcc, "_cgo_r = ")
628628
if c := tr.C.String(); c[len(c)-1] == '*' {
629-
fmt.Fprint(fgcc, "(__typeof__(a->r)) ")
629+
fmt.Fprint(fgcc, "(__typeof__(_cgo_a->r)) ")
630630
}
631631
}
632632
if n.Kind == "macro" {
@@ -637,7 +637,7 @@ func (p *Package) writeOutputFunc(fgcc *os.File, n *Name) {
637637
if i > 0 {
638638
fmt.Fprintf(fgcc, ", ")
639639
}
640-
fmt.Fprintf(fgcc, "a->p%d", i)
640+
fmt.Fprintf(fgcc, "_cgo_a->p%d", i)
641641
}
642642
fmt.Fprintf(fgcc, ");\n")
643643
}
@@ -648,9 +648,9 @@ func (p *Package) writeOutputFunc(fgcc *os.File, n *Name) {
648648
if n.FuncType.Result != nil {
649649
// The cgo call may have caused a stack copy (via a callback).
650650
// Adjust the return value pointer appropriately.
651-
fmt.Fprintf(fgcc, "\ta = (void*)((char*)a + (_cgo_topofstack() - stktop));\n")
651+
fmt.Fprintf(fgcc, "\t_cgo_a = (void*)((char*)_cgo_a + (_cgo_topofstack() - _cgo_stktop));\n")
652652
// Save the return value.
653-
fmt.Fprintf(fgcc, "\ta->r = r;\n")
653+
fmt.Fprintf(fgcc, "\t_cgo_a->r = _cgo_r;\n")
654654
}
655655
if n.AddError {
656656
fmt.Fprintf(fgcc, "\treturn _cgo_errno;\n")
@@ -685,12 +685,12 @@ func (p *Package) writeGccgoOutputFunc(fgcc *os.File, n *Name) {
685685
fmt.Fprintf(fgcc, ")\n")
686686
fmt.Fprintf(fgcc, "{\n")
687687
if t := n.FuncType.Result; t != nil {
688-
fmt.Fprintf(fgcc, "\t%s r;\n", t.C.String())
688+
fmt.Fprintf(fgcc, "\t%s _cgo_r;\n", t.C.String())
689689
}
690690
fmt.Fprintf(fgcc, "\t_cgo_tsan_acquire();\n")
691691
fmt.Fprintf(fgcc, "\t")
692692
if t := n.FuncType.Result; t != nil {
693-
fmt.Fprintf(fgcc, "r = ")
693+
fmt.Fprintf(fgcc, "_cgo_r = ")
694694
// Cast to void* to avoid warnings due to omitted qualifiers.
695695
if c := t.C.String(); c[len(c)-1] == '*' {
696696
fmt.Fprintf(fgcc, "(void*)")
@@ -716,7 +716,7 @@ func (p *Package) writeGccgoOutputFunc(fgcc *os.File, n *Name) {
716716
if c := t.C.String(); c[len(c)-1] == '*' {
717717
fmt.Fprintf(fgcc, "(void*)")
718718
}
719-
fmt.Fprintf(fgcc, "r;\n")
719+
fmt.Fprintf(fgcc, "_cgo_r;\n")
720720
}
721721
fmt.Fprintf(fgcc, "}\n")
722722
fmt.Fprintf(fgcc, "\n")

0 commit comments

Comments
 (0)