Skip to content

encoding/xml: panics with Field index out of range when XMLName is in embedded struct #16497

Open
@SamWhited

Description

@SamWhited

Please answer these questions before submitting your issue. Thanks!

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

go version devel +67f799c Tue Jul 26 00:18:42 2016 +0000 linux/amd64

But it also affects the version of 1.6 on the playground at the moment.

  1. What operating system and processor architecture are you using (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"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build800221808=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"
  1. What did you do?

When attempting to unmarshal XML into a struct that is composed of another struct that has an XMLName field that has a field number greater than or equal to the number of fields in the original struct, finfo.Value panics because reflect tries to get the value of the field in the parent struct at the location of XMLName in the child struct (which has a field index that is out of bounds when applied to the parent struct).

For example, this contrived example panics:

package main

import (
    "encoding/xml"
)

type IQ struct {
    Type    string   `xml:"type,attr"`
    XMLName xml.Name `xml:"iq"`
}

func main() {
    resp := struct {
        IQ
    }{}
    xml.Unmarshal([]byte(`<iq/>`), &resp)
}

(Playground)

but if we move the fields around:

type IQ struct {
    XMLName xml.Name `xml:"iq"`
    Type    string   `xml:"type,attr"`
}

it works (Playground)

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.early-in-cycleA change that should be done early in the 3 month dev cycle.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions