1. Xerces-C++
  2. XERCESC-1984

TranscodeToStr::transcode throws an exception when transcoding to UTF-8


    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 3.2.0, 4.0.0
    • Fix Version/s: 3.1.2, 3.2.0
    • Component/s: Utilities
    • Environment:
      Bug reproducible on a Red Hat 5 based platform. The bug doesn't seem to be platform specific though.


      This issue relates to the bug fix for issue XERCESC-1947. There are still cases where the method will fail in providing a transcoded version without throwing an exception. See the attached "transtest2.cpp" to reproduce the issue.

      The cause seems to come from the added "if((allocSize - fBytesWritten) < (len - charsDone))" condition in "TranscodeToStr::transcode" . In my provided test case I have a string composed of 6 Japanese characters (i.e. "絞り込み検索"). Once the first call to "XMLUTF8Transcoder::transcodeTo" is done, "charsRead" will return a count of 5 XMLCh readed. Since the initial allocated buffer for this string was set to 16 bytes, the condition will check against the following values "if((16 - 15) < (6 - 5))" which avoids the reallocation of a larger buffer for the UTF-8 encoded version of the string.

      Since the reallocation doesn't take place, the code will recall "XMLUTF8Transcoder::transcodeTo" but this time the "charsRead" count will be set to 0 because there is insufficient space in the buffer and this will trigger an exception of type "Trans_BadSrcSeq".

      I suppose that the goal of this added condition was to avoid an unnecessary reallocation of a buffer but unfortunately it doesn’t work when transcoding to variable length encoding like UTF-8. The solution is probably to simply replace the condition with "if(charsDone < len)".


        Issue Links


          Scott Cantor made changes -
          Status Resolved [ 5 ] Closed [ 6 ]
          Scott Cantor made changes -
          Fix Version/s 3.1.2 [ 12315014 ]
          Alberto Massari made changes -
          Status Open [ 1 ] Resolved [ 5 ]
          Assignee Alberto Massari [ amassari ]
          Fix Version/s 3.2.0 [ 12314395 ]
          Resolution Fixed [ 1 ]
          Lee Doron made changes -
          Link This issue relates to XERCESC-1987 [ XERCESC-1987 ]
          Dan PV made changes -
          Link This issue is related to XERCESC-1947 [ XERCESC-1947 ]
          Dan PV made changes -
          Field Original Value New Value
          Attachment transtest2.cpp [ 12527523 ]
          Dan PV created issue -


            • Assignee:
              Alberto Massari
              Dan PV
            • Votes:
              1 Vote for this issue
              4 Start watching this issue


              • Created: