Skip to content

cmd/cgo: mishandles untyped constants in some cases #30527

@remyoudompheng

Description

@remyoudompheng

What version of Go are you using (go version)?

$ go version
go version go1.12 linux/amd64

Does this issue reproduce with the latest release?

Yes, but it does not happen with go 1.11.5

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/remy/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/remy/travail/go:/storage/remy/gopath"
GOPROXY=""
GORACE=""
GOROOT="/usr/lib/go"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build301410002=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Create a 2-file package which invokes a C function with untyped constants.

% cat a.go
package pkg

import "fmt"
import "math"

/*
#include 

typedef struct {
	uint64_t a;
	uint64_t b;
	void *next;
} T;

uint64_t mul(T *x, uint64_t mod, uint32_t unused) {
	return (x->a * x->b);
}
*/
import "C"

func F() {
	t := &C.T{a: 1, b: 2, next: nil}

	n := C.mul(t, math.MaxUint64, 1)
	fmt.Println(n)
}

func G() {
	t := &C.T{a: 1, b: 2, next: nil}

	n := C.mul(t, 1<<64-1, z)
	fmt.Println(n)
}

% cat b.go
package pkg

const (
	x = 1 << iota
	y
	z
)

What did you expect to see?

Successful compilation (as with go1.11.5)

What did you see instead?

./a.go:24:94: cannot use _cgo1 (type int) as type _Ctype_ulong in argument to _Cfunc_mul
./a.go:24:22: constant 18446744073709551615 overflows int
./a.go:31:70: cannot use _cgo2 (type int) as type _Ctype_uint in argument to _Cfunc_mul

The error disappears if MaxUint64 is defined in the same package, or if z is defined in the same file.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions