xiehuachun / protobuf-java-format

Automatically exported from code.google.com/p/protobuf-java-format
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

invalid escapes \a and \v in output of byte array #43

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1.  Snippet from my test program:
        //  javascript in browser gets byte string embedded in larger message, using ajax.  if the
        //  data array has 0x07 or 0x0b, browser throws exception for the ajax thread.  please see the
        //  standard ECMA-262, at "http://www.ecma-international.org/publications/standards/Ecma-262.htm",
        //  section 15.12.1.1, titled "The JSON Lexical Grammar".  That section clearly states:
        //
        //      JSONEscapeCharacter  :: one of       " / \ b f n r t
        //
        //  There is no mention of "\a" or "\v" as legal escape shortcuts in a JSON string literal.
        //  

        //  snippet from .proto file ..
        //  
        //    required uint64        data_id = 1;
        //    required uint64        event_id = 2;
        //    optional bytes         data = 3;
        //  

        //  note .. in C-language (or, at least, Microsoft C compilers), there are extra
        //  escapes allowed:
        //
        //      \a      ==>  0x07       "alert"         (ascii BEL)
        //      \v      ==>  0x0b       "vertical tab"  (ascii VT)
        //
        byte[]  caseAbytes =  { 0x06, 0x07, 0x08 };     //  expect "\u0006\u0007\b", get "\u0006\a\b"
        byte[]  caseVbytes =  { 0x0a, 0x0b, 0x0c };     //  expect "\n\u0016\f", get "\n\v\f"

        ByteString  caseA =  ByteString.copyFrom( caseAbytes );
        ByteString  caseV =  ByteString.copyFrom( caseVbytes );

        //  The field "output_event_data" is a submessage of type EventData.  We
        //  want to alter the field "data" in that submessage.
        //
        //        at ..
        //
        DynamicMessage.Builder  editorProto =  parsedProto.newBuilder( parsedProto );
        DynamicMessage.Builder  editorOED =  editorProto.newBuilderForField( fieldOED );

        fieldOED_data_id =  fieldOED.getMessageType().findFieldByName( "data_id" );     
        fieldOED_event_id =  fieldOED.getMessageType().findFieldByName( "event_id" );
        fieldOED_data =  fieldOED.getMessageType().findFieldByName( "data" );

        try
        {
            Message  patchProto =  null;

            editorOED.setField( fieldOED_data_id, new Long(1) );
            editorOED.setField( fieldOED_event_id, new Long(2) );

            editorOED.setField( fieldOED_data, caseA );
            patchProto =  editorOED.build();

            if  (DUMP_CASE_A)
            {
                String  formatCaseA =  JsonFormat.printToString( patchProto );
                System.out.println( "illegal escape, demo case \\a: " + formatCaseA );
                System.out.println( "  ** should have been: \\u0006\\u0007\\b   **" );
            }

            editorOED =  editorProto.newBuilderForField( fieldOED );
            editorOED.setField( fieldOED_data_id, new Long(3) );
            editorOED.setField( fieldOED_event_id, new Long(4) );

            editorOED.setField( fieldOED_data, caseV );
            patchProto =  editorOED.build();

            if  (DUMP_CASE_V)
            {
                String  formatCaseV =  JsonFormat.printToString( patchProto );
                System.out.println( "illegal escape, demo case \\v: " + formatCaseV );
                System.out.println( "  ** should have been: \\n\\u0011\\f   **" );
            }
        }
        catch  (Exception  e)
        {
            e.printStackTrace();
        }

2. Wrap the snippet above in a compilable java source and run it.

The println() calls and comments document what should happen.
Impact if using ajax to feed ByteString to browser is that a browser such as 
Firefox complains about "invalid escaped character".

Using 1.2 release on linux 64-bit Centos 6.2 build.

Please provide any additional information below:

You can see in the source at 
"http://code.google.com/p/protobuf-java-format/source/browse/trunk/src/main/java
/com/googlecode/protobuf/format/JsonFormat.java",  line 1061, method 
escapeBytes() that someone ignored a strong hint about \a and \v and went ahead 
to (improperly) escape them for JSON output.  

Original issue reported on code.google.com by paul.cl...@ll.mit.edu on 14 Sep 2012 at 4:42

GoogleCodeExporter commented 9 years ago
Indeed, this was fixed for text values (issue 11). It seems it wasn't for byte 
values.

Patch welcomed...

Original comment by philippe...@gmail.com on 16 Sep 2012 at 4:16

GoogleCodeExporter commented 9 years ago
i have fixed the issue. it would be great to release new version asap.

Original comment by korolyov...@gmail.com on 23 Jan 2013 at 9:57

Attachments:

GoogleCodeExporter commented 9 years ago
Is there any place to get this fix as part of a built jar file?

Original comment by cka...@gmail.com on 14 Aug 2014 at 7:44

GoogleCodeExporter commented 9 years ago
I built the jar from the source code. However, I could not get the test code to 
build.
I also had to add a similar fix for escaping the single quote '

Original comment by cka...@gmail.com on 15 Aug 2014 at 11:51