Uploaded image for project: 'QuickFIX/J'
  1. QuickFIX/J
  2. QFJ-282

FIXMessageEncoder#encode() may throws java.nio.BufferOverflowException if message contains Chinese characters

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Default
    • Resolution: Duplicate
    • Affects Version/s: 1.2.1
    • Fix Version/s: 1.6.0
    • Component/s: Engine
    • Labels:

      Description

      FIXMessageEncoder#encode() may throws protocol handler exception: java.nio.BufferOverflowException, if message contains Chinese characters.

      we use "UTF-8" as default charset instead of "ISO-8859-1". So charsetEncoding = "UTF-8".

      FIXMessageEncoder#encode() :
      ByteBuffer buffer = ByteBuffer.allocate(fixMessageString.length());
      try

      { buffer.put(fixMessageString.getBytes(charsetEncoding)); }

      catch (UnsupportedEncodingException e)

      { throw new ProtocolCodecException(e); }

      if message contains Chinese characters, fixMessageString.length() will not equal to fixMessageString.getBytes(charsetEncoding).length. Because A Chinese character will use "three" Bytes in "UTF-8". But in String, it's length is still "1".

      we let: byte[] src = fixMessageString.getBytes(charsetEncoding);
      when a message contains "one" Chinese character,
      if fixMessageString.length() = 190, then src.length = 192, buffer.capacity() = 256. So buffer.put(src) will not throw java.nio.BufferOverflowException;
      if fixMessageString.length() = 255, then src.length = 257, buffer.capacity() = 256. At this time, buffer.put(src) will throw java.nio.BufferOverflowException;

      solution:
      use "fixMessageString.getBytes(charsetEncoding).length" to allocate buffer space instead of "fixMessageString.length()".

      ByteBuffer buffer;
      try

      { byte[] src = fixMessageString.getBytes(charsetEncoding); buffer = ByteBuffer.allocate(src.length); buffer.put(src); }

      catch (UnsupportedEncodingException e)

      { throw new ProtocolCodecException(e); }

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                amichair amichair
                Reporter:
                caiqi CaiQi
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: