Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8143366

Control characters in constant pool strings are not escaped properly

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 9
    • None
    • tools
    • b130
    • Verified

      Compile and java-p this:

      class Foo {
          static final String s = "\1\2\3";
          static String f() { return s; }
      }

      The tool outputs control characters which most terminals do not display.
      This is a problem when strings are used to carry binary data.
      Escapes are needed to visualize the class file contents in this case.
      Suggestion: four-digit unicode escapes, such as \u0001\u0002\u0003.

      Note that the classfile printer (used by the javap -constant option) displays all characters using \uNNNN, except 7-bit printables. This would be acceptable also, though it make it hard to see most unicode characters.

      In any case, control characters *must* be escaped.

      Suggested fix:

      diff --git a/src/jdk.jdeps/share/classes/com/sun/tools/javap/ConstantWriter.java b/src/jdk.jdeps/share/classes/com/sun/tools/javap/ConstantWriter.java
      --- a/src/jdk.jdeps/share/classes/com/sun/tools/javap/ConstantWriter.java
      +++ b/src/jdk.jdeps/share/classes/com/sun/tools/javap/ConstantWriter.java
      @@ -385,6 +385,11 @@
                               sb.append('\\').append('\\');
                               break;
                           default:
      + if (Character.isISOControl(c)) {
      + sb.append(String.format("\\u%04x", (int) c));
      + break;
      + }
      + // or copy logic of ClassWriter::esc = (c < 32 || c > 126)
                               sb.append(c);
                       }
                   }

            sdrach Steve Drach (Inactive)
            jrose John Rose
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: