Skip to content

Commit 2cfde41

Browse files
minuxaclements
authored andcommitted
[release-branch.go1.5] runtime/cgo: explicitly link msvcrt on windows
It's because runtime links to ntdll, and ntdll exports a couple incompatible libc functions. We must link to msvcrt first and then try ntdll. Fixes #12030. Change-Id: I0105417bada108da55f5ae4482c2423ac7a92957 Reviewed-on: https://go-review.googlesource.com/14472 Reviewed-by: Alex Brainman <[email protected]> Run-TryBot: Minux Ma <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-on: https://go-review.googlesource.com/16966 Run-TryBot: Austin Clements <[email protected]> Reviewed-by: Minux Ma <[email protected]> Reviewed-by: Russ Cox <[email protected]>
1 parent 095710b commit 2cfde41

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

misc/cgo/test/cgo_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,6 @@ func Test9026(t *testing.T) { test9026(t) }
6565
func Test9557(t *testing.T) { test9557(t) }
6666
func Test10303(t *testing.T) { test10303(t, 10) }
6767
func Test11925(t *testing.T) { test11925(t) }
68+
func Test12030(t *testing.T) { test12030(t) }
6869

6970
func BenchmarkCgoCall(b *testing.B) { benchCgoCall(b) }

misc/cgo/test/issue12030.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2015 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+
// Issue 12030. sprintf is defined in both ntdll and msvcrt,
6+
// Normally we want the one in the msvcrt.
7+
8+
package cgotest
9+
10+
/*
11+
#include <stdio.h>
12+
#include <stdlib.h>
13+
void issue12030conv(char *buf, double x) {
14+
sprintf(buf, "d=%g", x);
15+
}
16+
*/
17+
import "C"
18+
19+
import (
20+
"fmt"
21+
"testing"
22+
"unsafe"
23+
)
24+
25+
func test12030(t *testing.T) {
26+
buf := (*C.char)(C.malloc(256))
27+
defer C.free(unsafe.Pointer(buf))
28+
for _, f := range []float64{1.0, 2.0, 3.14} {
29+
C.issue12030conv(buf, C.double(f))
30+
got := C.GoString(buf)
31+
if want := fmt.Sprintf("d=%g", f); got != want {
32+
t.Fatalf("C.sprintf failed for %g: %q != %q", f, got, want)
33+
}
34+
}
35+
}

src/runtime/cgo/cgo.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ package cgo
2020
#cgo !android,linux LDFLAGS: -lpthread
2121
#cgo netbsd LDFLAGS: -lpthread
2222
#cgo openbsd LDFLAGS: -lpthread
23-
#cgo windows LDFLAGS: -lm -mthreads
23+
// we must explicitly link msvcrt, because runtime needs ntdll, and ntdll
24+
// exports some incompatible libc functions. See golang.org/issue/12030.
25+
#cgo windows LDFLAGS: -lmsvcrt -lm -mthreads
2426
2527
#cgo CFLAGS: -Wall -Werror
2628

0 commit comments

Comments
 (0)