Segmentation violation when accessing non-pointer passed to interface{}

116 views
Skip to first unread message

Shulhan

unread,
Oct 25, 2018, 9:09:58 AM10/25/18
to golan...@googlegroups.com
The following program result in SIGSEGV on linux,

package main

import (
"fmt"
)

type A struct {
K int
S string
M map[string]string
}

func newA(k int, s string) (a A) {
a.K = k
a.S = s
a.M = make(map[string]string)
a.M[s] = s
return
}

func proxy() (x int, a A) {
return 1, newA(2, "3")
}

func consume(x int, a interface{}) {
fmt.Println(x)
fmt.Println(a)
}

func main() {
consume(proxy())
}


== Using Go tip

20:00 master ms 1 % go version
go version devel +265303525c Thu Oct 25 19:51:46 2018 +0700 linux/amd64

20:04 ~/tmp/sandbox/go/src/bug/fparamf
master ms 0 % go run ./main.go
1
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x12 pc=0x482365]

goroutine 1 [running]:
fmt.(*pp).printArg(0xc00009e000, 0x2, 0x4b6292, 0x76)
/home/ms/Programs/go/src/fmt/print.go:648 +0x185
fmt.(*pp).doPrintln(0xc00009e000, 0xc00009af38, 0x1, 0x1)
/home/ms/Programs/go/src/fmt/print.go:1157 +0xb1
fmt.Fprintln(0x4cb1a0, 0xc00000e018, 0xc00009af38, 0x1, 0x1, 0x2, 0x0,
0x0) /home/ms/Programs/go/src/fmt/print.go:265 +0x58
fmt.Println(0xc00009af38, 0x1, 0x1, 0x2, 0x0, 0x0)
/home/ms/Programs/go/src/fmt/print.go:275 +0x57
main.consume(0x1, 0x2, 0x4b6292)
/home/ms/tmp/sandbox/go/src/bug/fparamf/main.go:27 +0xb3
main.main()
/home/ms/tmp/sandbox/go/src/bug/fparamf/main.go:31 +0xa7
exit status 2


== Using Go 1.11.1

20:04 ~/tmp/sandbox/go/src/bug/fparamf
master ms 1 % go version
go version go1.11.1 linux/amd64

20:06 ~/tmp/sandbox/go/src/bug/fparamf
master ms 0 % go run ./main.go
1
{2 3 map[3:3]}


Is there on going progress on compiler? I am hesitant to report this as
an issue on Github.

--
{ "github":"github.com/shuLhan", "site":"kilabit.info" }

Shulhan

unread,
Oct 25, 2018, 9:21:22 AM10/25/18
to golan...@googlegroups.com
On Thu, 25 Oct 2018 20:09:42 +0700
Shulhan <m.sh...@gmail.com> wrote:

> The following program result in SIGSEGV on linux,
>
> package main
>
> import (
> "fmt"
> )
>
> type A struct {
> K int
> S string
> M map[string]string
> }
>
> func newA(k int, s string) (a A) {
> a.K = k
> a.S = s
> a.M = make(map[string]string)
> a.M[s] = s
> return
> }
>
> func proxy() (x int, a A) {
> return 1, newA(2, "3")
> }
>
> func consume(x int, a interface{}) {
> fmt.Println(x)
> fmt.Println(a)
> }
>
> func main() {
> consume(proxy())
> }
>

But, if I save the returned result into variables, it will compile and
run successfully using Go tip,

...
func main() {
x, a := proxy()
consume(x, a)
}
...

20:17 ~/tmp/sandbox/go/src/bug/fparamf
master ms 0 % go version
go version devel +265303525c Thu Oct 25 19:51:46 2018 +0700 linux/amd64

20:17 ~/tmp/sandbox/go/src/bug/fparamf

cherry

unread,
Oct 25, 2018, 10:58:28 AM10/25/18
to m.sh...@gmail.com, golan...@googlegroups.com
This looks like a bug to me. Could you file an issue, so we continue from there?

It seems the conversion from A to interface is completely lost, in function main.


--
You received this message because you are subscribed to the Google Groups "golang-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

cherry

unread,
Oct 25, 2018, 11:09:30 AM10/25/18
to m.sh...@gmail.com, Josh Bleecher Snyder, golan...@googlegroups.com
On Thu, Oct 25, 2018 at 10:58 AM cherry <luna...@gmail.com> wrote:
This looks like a bug to me. Could you file an issue, so we continue from there?

It seems the conversion from A to interface is completely lost, in function main.

It seems the culprit is CL https://go-review.googlesource.com/c/114797. +Josh

Josh Bleecher Snyder

unread,
Oct 25, 2018, 12:46:19 PM10/25/18
to cherry, m.sh...@gmail.com, golan...@googlegroups.com
Thanks! Filed https://github.com/golang/go/issues/28390.

For future reference, issues are cheap, and we try to keep the
compiler+runtime stable, even during development, so don't hesitate to
file issues. Thanks again for finding this.

-josh
Reply all
Reply to author
Forward
0 new messages