You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by "Martin Leuschner (JIRA)" <ji...@apache.org> on 2012/08/14 11:45:37 UTC

[jira] [Created] (DIRMINA-904) TextLineCodecFactory does not decode correct if there is a nullbyte within the string

Martin Leuschner created DIRMINA-904:
----------------------------------------

             Summary: TextLineCodecFactory does not decode correct if there is a nullbyte within the string
                 Key: DIRMINA-904
                 URL: https://issues.apache.org/jira/browse/DIRMINA-904
             Project: MINA
          Issue Type: Bug
          Components: Filter
    Affects Versions: 2.0.4
         Environment: Ubuntu Linux, Java 1.6.0_26, Mina 2.0.4
            Reporter: Martin Leuschner


I am running a tcp+udp server using mina. As decoder i am using  TextLineCodecFactory. Some of the messages we receive contain a nullbyte at the beginning. In that case the received message is an empty string, so the message gets lost.

Example:
//acceptor:
acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory()));
acceptor.setHandler(myHandler);

//test message with additional nullbyte at the beginning
String s = new String(new byte[] {0, 77, 105, 110, 97});  //nullbyte + "Mina"

//now send s to mina
//myHandler:
public void messageReceived(IoSession session, Object message) throws Exception {
		String res = (String) message;  //length is 0 instead of 5
}

See attached maven-project with unit-test






--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

[jira] [Commented] (DIRMINA-904) TextLineCodecFactory does not decode correct if there is a nullbyte within the string

Posted by "Emmanuel Lecharny (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/DIRMINA-904?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13434038#comment-13434038 ] 

Emmanuel Lecharny commented on DIRMINA-904:
-------------------------------------------

I have a patch that fixes this behavior :


### Eclipse Workspace Patch 1.0
#P mina-core-2.0.5
Index: src/main/java/org/apache/mina/filter/codec/textline/TextLineDecoder.java
===================================================================
--- src/main/java/org/apache/mina/filter/codec/textline/TextLineDecoder.java	(revision 1369898)
+++ src/main/java/org/apache/mina/filter/codec/textline/TextLineDecoder.java	(working copy)
@@ -270,17 +265,18 @@
                     IoBuffer buf = ctx.getBuffer();
                     buf.flip();
                     buf.limit(buf.limit() - matchCount);
-                    
+
                     try {
-                        writeText(session, buf.getString(ctx.getDecoder()), out);
+                        byte[] data = new byte[buf.limit()];
+                        buf.get(data);
+                        writeText(session, new String(data, ctx.getDecoder().charset()), out);
                     } finally {
                         buf.clear();
                     }
                 } else {
                     int overflowPosition = ctx.getOverflowPosition();
                     ctx.reset();
-                    throw new RecoverableProtocolDecoderException(
-                            "Line is too long: " + overflowPosition);
+                    throw new RecoverableProtocolDecoderException("Line is too long: " + overflowPosition);
                 }
 
                 oldPos = pos;

                
> TextLineCodecFactory does not decode correct if there is a nullbyte within the string
> -------------------------------------------------------------------------------------
>
>                 Key: DIRMINA-904
>                 URL: https://issues.apache.org/jira/browse/DIRMINA-904
>             Project: MINA
>          Issue Type: Bug
>          Components: Filter
>    Affects Versions: 2.0.4
>         Environment: Ubuntu Linux, Java 1.6.0_26, Mina 2.0.4
>            Reporter: Martin Leuschner
>         Attachments: mina_nullbyte_bug.zip
>
>
> I am running a tcp+udp server using mina. As decoder i am using  TextLineCodecFactory. Some of the messages we receive contain a nullbyte at the beginning. In that case the received message is an empty string, so the message gets lost.
> Example:
> //acceptor:
> acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory()));
> acceptor.setHandler(myHandler);
> //test message with additional nullbyte at the beginning
> String s = new String(new byte[] {0, 77, 105, 110, 97});  //nullbyte + "Mina"
> //now send s to mina
> //myHandler:
> public void messageReceived(IoSession session, Object message) throws Exception {
> 		String res = (String) message;  //length is 0 instead of 5
> }
> See attached maven-project with unit-test

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

[jira] [Commented] (DIRMINA-904) TextLineCodecFactory does not decode correct if there is a nullbyte within the string

Posted by "Emmanuel Lecharny (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/DIRMINA-904?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13434037#comment-13434037 ] 

Emmanuel Lecharny commented on DIRMINA-904:
-------------------------------------------

This is on purpose.

The decoder tries to convert the data into a String using the IoBuffer.getString( CharSetDecoder ) method, which says :
"Reads a NUL-terminated string from this buffer using the specified decoder and returns it. This method reads until the limit of this buffer if no NUL is found."

So if the first character is NULL, you will get an empty string as a result. 

Let me check if we can handle such String differently in the code...
                
> TextLineCodecFactory does not decode correct if there is a nullbyte within the string
> -------------------------------------------------------------------------------------
>
>                 Key: DIRMINA-904
>                 URL: https://issues.apache.org/jira/browse/DIRMINA-904
>             Project: MINA
>          Issue Type: Bug
>          Components: Filter
>    Affects Versions: 2.0.4
>         Environment: Ubuntu Linux, Java 1.6.0_26, Mina 2.0.4
>            Reporter: Martin Leuschner
>         Attachments: mina_nullbyte_bug.zip
>
>
> I am running a tcp+udp server using mina. As decoder i am using  TextLineCodecFactory. Some of the messages we receive contain a nullbyte at the beginning. In that case the received message is an empty string, so the message gets lost.
> Example:
> //acceptor:
> acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory()));
> acceptor.setHandler(myHandler);
> //test message with additional nullbyte at the beginning
> String s = new String(new byte[] {0, 77, 105, 110, 97});  //nullbyte + "Mina"
> //now send s to mina
> //myHandler:
> public void messageReceived(IoSession session, Object message) throws Exception {
> 		String res = (String) message;  //length is 0 instead of 5
> }
> See attached maven-project with unit-test

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

[jira] [Updated] (DIRMINA-904) TextLineCodecFactory does not decode correct if there is a nullbyte within the string

Posted by "Martin Leuschner (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/DIRMINA-904?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Martin Leuschner updated DIRMINA-904:
-------------------------------------

    Attachment: mina_nullbyte_bug.zip

maven project with unit test showing the behavior
                
> TextLineCodecFactory does not decode correct if there is a nullbyte within the string
> -------------------------------------------------------------------------------------
>
>                 Key: DIRMINA-904
>                 URL: https://issues.apache.org/jira/browse/DIRMINA-904
>             Project: MINA
>          Issue Type: Bug
>          Components: Filter
>    Affects Versions: 2.0.4
>         Environment: Ubuntu Linux, Java 1.6.0_26, Mina 2.0.4
>            Reporter: Martin Leuschner
>         Attachments: mina_nullbyte_bug.zip
>
>
> I am running a tcp+udp server using mina. As decoder i am using  TextLineCodecFactory. Some of the messages we receive contain a nullbyte at the beginning. In that case the received message is an empty string, so the message gets lost.
> Example:
> //acceptor:
> acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory()));
> acceptor.setHandler(myHandler);
> //test message with additional nullbyte at the beginning
> String s = new String(new byte[] {0, 77, 105, 110, 97});  //nullbyte + "Mina"
> //now send s to mina
> //myHandler:
> public void messageReceived(IoSession session, Object message) throws Exception {
> 		String res = (String) message;  //length is 0 instead of 5
> }
> See attached maven-project with unit-test

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

[jira] [Resolved] (DIRMINA-904) TextLineCodecFactory does not decode correct if there is a nullbyte within the string

Posted by "Emmanuel Lecharny (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/DIRMINA-904?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Emmanuel Lecharny resolved DIRMINA-904.
---------------------------------------

    Resolution: Fixed

I applied the patch : http://svn.apache.org/viewvc?rev=1374997&view=rev
                
> TextLineCodecFactory does not decode correct if there is a nullbyte within the string
> -------------------------------------------------------------------------------------
>
>                 Key: DIRMINA-904
>                 URL: https://issues.apache.org/jira/browse/DIRMINA-904
>             Project: MINA
>          Issue Type: Bug
>          Components: Filter
>    Affects Versions: 2.0.4
>         Environment: Ubuntu Linux, Java 1.6.0_26, Mina 2.0.4
>            Reporter: Martin Leuschner
>         Attachments: mina_nullbyte_bug.zip
>
>
> I am running a tcp+udp server using mina. As decoder i am using  TextLineCodecFactory. Some of the messages we receive contain a nullbyte at the beginning. In that case the received message is an empty string, so the message gets lost.
> Example:
> //acceptor:
> acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory()));
> acceptor.setHandler(myHandler);
> //test message with additional nullbyte at the beginning
> String s = new String(new byte[] {0, 77, 105, 110, 97});  //nullbyte + "Mina"
> //now send s to mina
> //myHandler:
> public void messageReceived(IoSession session, Object message) throws Exception {
> 		String res = (String) message;  //length is 0 instead of 5
> }
> See attached maven-project with unit-test

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira