Skip to content

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

Description

@GoogleCodeExporter
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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions