xmlrpc
xmlrpc copied to clipboard
Fix array parsing when one element is without type identifier
Hello, i stumbled across a response that xmlrpc is unable to decode correctly:
<value><array><data>
<value><int>5</int></value>
<value></value>
<value><string>A</string></value>
</data></array></value>
This is a 3-elements array containing respectively:
- an int
- a value without the type identifier, which according to the XML-RPC specification is acceptable (http://xmlrpc.scripting.com/spec.html), and is also normally handled by xmlrpc (https://github.com/kolo/xmlrpc/blob/master/decoder_test.go#L138)
- a string
The library decodes the XML into a 2-elements slice:
{ 5, nil }
I did some tests, and i discovered that, when decoding a slice, the library does not decode correctly the element immediately after an element without the type identifier. That's due to this sequence of events:
-
the slice decoder calls dec.Token() and reads a token (<value>) https://github.com/kolo/xmlrpc/blob/master/decoder.go#L248
-
the slice decoder calls dec.decodeValue() https://github.com/kolo/xmlrpc/blob/master/decoder.go#L266
-
dec.decodeValue() calls dec.Token() and reads a token, expecting to find the starting tag of a type identifier (i.e. <string>), but instead it finds </value>, and returns nil, since it's an EndElement https://github.com/kolo/xmlrpc/blob/master/decoder.go#L89
-
the slice decoder calls dec.Skip(), which normally should read any tag up to </value>, but since </value> has already been processed, it goes on and reads the next <value>, <string>, </string>, </value>, so the next array element is totally skipped: https://github.com/kolo/xmlrpc/blob/master/decoder.go#L278
This patch fixes the problem, by skipping dec.Skip() when this problem arises. I also added a unit test.
I'll try to get to this by the end of the week.