Skip to content
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

encoding/xml: segfault when unmarshaling struct containing a name field with ,attr tag #20953

Closed
SamWhited opened this Issue Jul 8, 2017 · 2 comments

Comments

Projects
None yet
3 participants
@SamWhited
Copy link
Member

SamWhited commented Jul 8, 2017

I haven't looked into this, but removing the ,attr tag makes it work. Having the ,attr tag there doesn't make sense anyways, but I would expect it to return an error, not segfault.

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

$ go version
go version devel +7a89474fe1 Tue Jul 4 22:55:46 2017 -0500 linux/amd64

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

$ go env
GOARCH="amd64"
GOBIN="/home/sam/bin"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/sam"
GORACE=""
GOROOT="/home/sam/src/go"
GOTOOLDIR="/home/sam/src/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build927551460=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

What did you do?

package main

import (
	"encoding/xml"
	"log"
	"os"
)

type CancelError struct {
	XMLName xml.Name `error"`
	Type    struct {
		XMLName xml.Name `xml:"type,attr"`
	}
}

func main() {
	e := xml.NewEncoder(os.Stdout)
	if err := e.Encode(CancelError{}); err != nil {
		log.Fatal(err)
	}
}

Playground

What did you expect to see?

XML output to stdout or an error returned if the struct was not able to be marshaled. Maybe an "xml: unsupported type" error similar to trying to marshal:

type CancelError struct {
	XMLName xml.Name `xml:"urn:xmpp:err error"`
	Type    struct{} `xml:"type,attr"`
}

What did you see instead?

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0xffffffff addr=0x0 pc=0xf304a]

goroutine 1 [running]:
encoding/xml.lookupXMLName(0x1a2c60, 0x114ae0, 0x1280ea, 0x1)
	/usr/local/go/src/encoding/xml/typeinfo.go:246 +0x2ca
encoding/xml.structFieldInfo(0x1a2c60, 0x118400, 0x10529d94, 0x3, 0x0, 0x0, 0x1a2c60, 0x114ae0)
	/usr/local/go/src/encoding/xml/typeinfo.go:192 +0x5c0
encoding/xml.getTypeInfo(0x1a2c60, 0x118400, 0x115c60, 0x23, 0x1a2c00, 0x118400)
	/usr/local/go/src/encoding/xml/typeinfo.go:92 +0x3e0
encoding/xml.(*printer).marshalValue(0x1054e050, 0x118400, 0x105402e0, 0x99, 0x0, 0x0, 0x1054e050, 0x105402e0)
	/usr/local/go/src/encoding/xml/marshal.go:455 +0x2c0
encoding/xml.(*Encoder).Encode(0x1054e050, 0x118400, 0x105402e0, 0x105402e0, 0x10564000, 0x1000)
	/usr/local/go/src/encoding/xml/marshal.go:155 +0x80
main.main()
	/tmp/sandbox323847863/main.go:18 +0x120
@SamWhited

This comment has been minimized.

Copy link
Member Author

SamWhited commented Jul 8, 2017

/cc @rsc

@ALTree ALTree added this to the Go1.10 milestone Jul 11, 2017

@gopherbot

This comment has been minimized.

Copy link

gopherbot commented Dec 7, 2017

Change https://golang.org/cl/82397 mentions this issue: encoding/xml: don't crash on invalid XMLName tag

@gopherbot gopherbot closed this in 89d7a2f Dec 7, 2017

@golang golang locked and limited conversation to collaborators Dec 7, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.