From 6adf54a3ebca857ae529b78c03945750731042ed Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Fri, 10 Jan 2025 16:35:24 -0800 Subject: [PATCH] cmd/cgo: declare _GoString{Len,Ptr} in _cgo_export.h Fixes #71226 Change-Id: I91c46a4310a9c7a9fcd1e3a131ca16e46949edb3 Reviewed-on: https://go-review.googlesource.com/c/go/+/642235 Auto-Submit: Ian Lance Taylor Reviewed-by: David Chase LUCI-TryBot-Result: Go LUCI Reviewed-by: Ian Lance Taylor --- src/cmd/cgo/out.go | 2 ++ test/fixedbugs/issue71226.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 test/fixedbugs/issue71226.go diff --git a/src/cmd/cgo/out.go b/src/cmd/cgo/out.go index b3e4c7ccdf..36a0267713 100644 --- a/src/cmd/cgo/out.go +++ b/src/cmd/cgo/out.go @@ -1938,6 +1938,8 @@ const builtinExportProlog = ` #ifndef GO_CGO_GOSTRING_TYPEDEF typedef struct { const char *p; ptrdiff_t n; } _GoString_; +extern size_t _GoStringLen(_GoString_ s); +extern const char *_GoStringPtr(_GoString_ s); #endif #endif diff --git a/test/fixedbugs/issue71226.go b/test/fixedbugs/issue71226.go new file mode 100644 index 0000000000..704814b601 --- /dev/null +++ b/test/fixedbugs/issue71226.go @@ -0,0 +1,29 @@ +// build + +//go:build cgo + +// Copyright 2025 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package main + +/* +#cgo CFLAGS: -Werror -Wimplicit-function-declaration + +#include + +static void CFn(_GoString_ gostr) { + printf("%.*s\n", _GoStringLen(gostr), _GoStringPtr(gostr)); +} +*/ +import "C" + +func main() { + C.CFn("hello, world") +} + +// The bug only occurs if there is an exported function. +//export Fn +func Fn() { +} -- 2.51.0