kolo / xmlrpc

Implementation of XMLRPC protocol in Go language.
MIT License
159 stars 94 forks source link

Fix array parsing when one element is without type identifier #67

Closed aler9 closed 9 months ago

aler9 commented 5 years ago

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:

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:

  1. the slice decoder calls dec.Token() and reads a token (<value>) https://github.com/kolo/xmlrpc/blob/master/decoder.go#L248

  2. the slice decoder calls dec.decodeValue() https://github.com/kolo/xmlrpc/blob/master/decoder.go#L266

  3. 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

  4. 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.

icholy commented 5 years ago

I'll try to get to this by the end of the week.