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

java.net.URLDecode.decode(st,"UTF-16") works incorrectly on '+' sign

XMLWordPrintable

    • beta
    • sparc
    • solaris_2.6



      Name: dfR10049 Date: 01/24/2001



      URLDecode.decode(st,"UTF-16") works incorrectly on '+' sign. If this sign occures in
      the encoded string and the following sequence contains encoded unsafe characters (%xy)
      these characters will be decoded incorrectly.

      Please see an example demonstrating the bug below:
      ----------- DecoderTest.java ----------------
      import java.net.*;

      public class Decoder {

         public static void main(String args[]) {

              boolean passed = true;
              String enc = "UTF-16";
              String strings[] = {
                  "\u0100\u0101",
                  "\u0100 \u0101",
                  "\u0100 \u0101\u0102",
                  "\u0100 \u0101 \u0102",
                  "\u0100\u0101\u0102",
              };

              try {
                   for (int i = 0; i < strings.length; i++) {
                       String encoded = URLEncoder.encode(strings[i], enc);
                       System.out.println("ecnoded: " + encoded);
                       String decoded = URLDecoder.decode(encoded, enc);
                       System.out.print("init: ");
                       printString(strings[i]);
                       System.out.print("decoded: ");
                       printString(decoded);
                       if (strings[i].equals(decoded)) {
                            System.out.println(" - correct - \n");
                       } else {
                            System.out.println(" - incorrect - \n");
                            passed = false;
                       }
                   }
              } catch (Exception e) {
                  System.out.println(" exception: " + e);
              }

              System.out.println("Test " + (passed ? "passed" : "failed"));
          }

          static void printString(String s) {
               for (int i = 0; i < s.length(); i++) {
                    System.out.print((int)s.charAt(i) + " ");
               }
               System.out.println();
          }
      }
      #----------------- output from the test ----------------------

      #> java -version
      java version "1.4.0-beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta-b48)
      Java HotSpot(TM) Client VM (build 1.4beta-B48, mixed mode)

      #> java Decoder
      ecnoded: %FF%FE%00%01%01%01
      init: 256 257
      decoded: 256 257
       - correct -

      ecnoded: %FF%FE%00%01+%01%01
      init: 256 32 257
      decoded: 256 32 65533
       - incorrect -

      ecnoded: %FF%FE%00%01+%01%01%02%01
      init: 256 32 257 258
      decoded: 256 32 65533
       - incorrect -

      ecnoded: %FF%FE%00%01+%01%01+%02%01
      init: 256 32 257 32 258
      decoded: 256 32 65533 32 65533
       - incorrect -

      ecnoded: %FF%FE%00%01%01%01%02%01
      init: 256 257 258
      decoded: 256 257 258
       - correct -

      Test failed

      #-------------------------------------------------------------
      ======================================================================

            michaelm Michael McMahon
            fdasunw Fda Fda (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: