]> granicus.if.org Git - icu/commitdiff
ICU-12956 fix buffer overrun of UTF-7 and IMAP-mailbox-name
authorFrank Tang <ftang@chromium.org>
Tue, 12 Mar 2019 04:36:22 +0000 (21:36 -0700)
committerFrank Yung-Fong Tang <41213225+FrankYFTang@users.noreply.github.com>
Fri, 15 Mar 2019 03:00:41 +0000 (20:00 -0700)
icu4j/main/classes/charset/src/com/ibm/icu/charset/CharsetUTF7.java
icu4j/main/tests/charset/src/com/ibm/icu/dev/test/charset/TestCharset.java

index 3f2cb4972a3f1c0fe80358b783da729c061ca273..345870c72842fd8d81febbb44ee32b4b986d931d 100644 (file)
@@ -28,7 +28,7 @@ class CharsetUTF7 extends CharsetICU {
 
     public CharsetUTF7(String icuCanonicalName, String javaCanonicalName, String[] aliases) {
         super(icuCanonicalName, javaCanonicalName, aliases);
-        maxBytesPerChar=4; /* max 3 bytes per code unit from UTF-7 (base64) */
+        maxBytesPerChar=5; /* max 3 bytes per code unit from UTF-7 (base64) plus SIN SOUT */
         minBytesPerChar=1;
         maxCharsPerByte=1;
 
index 5ffdd41a258f98b198aaaa52cb88597d3ccf127a..f0f8850748210adbc3f74095a7a861489c3d0e38 100644 (file)
@@ -21,6 +21,7 @@ import java.nio.charset.CodingErrorAction;
 import java.nio.charset.UnsupportedCharsetException;
 import java.nio.charset.spi.CharsetProvider;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Iterator;
 import java.util.MissingResourceException;
 import java.util.Set;
@@ -3217,6 +3218,28 @@ public class TestCharset extends TestFmwk {
         //end of charset encoder code coverage code
     }
 
+    @Test
+    public void TestBug12956() {
+        final CharsetProvider provider = new CharsetProviderICU();
+        final Charset cs_utf7 = provider.charsetForName("UTF-7");
+        final Charset cs_imap = provider.charsetForName("IMAP-mailbox-name");
+        final String test = "新";
+        final byte[] expected_utf7 = {0x2b, 0x5a, 0x62, 0x41, 0x2d};
+        final byte[] expected_imap = {0x26, 0x5a, 0x62, 0x41, 0x2d};
+
+        byte[] bytes = test.getBytes(cs_utf7);
+        if (!Arrays.equals(bytes, expected_utf7)) {
+            errln("Incorrect UTF-7 conversion. Got " + new String(bytes) + " but expect " +
+                  new String(expected_utf7));
+        }
+
+        bytes = test.getBytes(cs_imap);
+        if (!Arrays.equals(bytes, expected_imap)) {
+            errln("Incorrect IMAP-mailbox-name conversion. Got " + new String(bytes) +
+                  " but expect " + new String(expected_imap));
+        }
+    }
+
     //Test Charset ISCII
     @Test
     public void TestCharsetISCII() {