Skip to content

Variadic parameters are mangled passed down the function stack #51582

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
markdiener10 opened this issue Mar 10, 2022 · 2 comments
Closed

Variadic parameters are mangled passed down the function stack #51582

markdiener10 opened this issue Mar 10, 2022 · 2 comments

Comments

@markdiener10
Copy link

markdiener10 commented Mar 10, 2022

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

$ go version

1.17.8

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env

GO111MODULE=""
GOARCH="arm64"
GOBIN=""
GOCACHE="/Users/marco/Library/Caches/go-build"
GOENV="/Users/marco/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="arm64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/marco/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/marco/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/opt/homebrew/Cellar/go/1.17.8/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/opt/homebrew/Cellar/go/1.17.8/libexec/pkg/tool/darwin_arm64"
GOVCS=""
GOVERSION="go1.17.8"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/sdev/golang/abds-go/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/03/csncw3x15h54rjwv14wsf6x40000gn/T/go-build2466302340=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Passing variadic parameters further down the function call stack mangles the parameter.

Run this in the Go Playground:

package main

import (
"fmt"
)

func one(parms ...interface{}) {

fmt.Println("One#######")

for idx,parm := range parms {
	fmt.Println(idx,parm)	
}
two(parms)

}

func two(parms ...interface{}) {

fmt.Println("Two#######")

for idx,parm := range parms {
	fmt.Println(idx,parm)	
}

}

func main() {
one(11,12,13)
}

What did you expect to see?

One#######
0 11
1 12
2 13
Two#######
0 11
1 12
2 13

What did you see instead?

One#######
0 11
1 12
2 13
Two#######
0 [11 12 13]

@mpx
Copy link
Contributor

mpx commented Mar 10, 2022

The code is working correctly. The example has a bug given your intent: two(parms) needs to be two(parms...).

Otherwise you are passing the slice of arguments as the first parameter to two. Hence you see the slice output [11 12 13].

@markdiener10
Copy link
Author

When calling the two( ) function, you must add the spread operator

two(parms...)

Then it works ok.

@golang golang locked and limited conversation to collaborators Mar 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants