Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 590ab791 authored by cketti's avatar cketti Committed by GitHub
Browse files

Merge pull request #6810 from thundernest/charset_crash

Fix bug in `CharsetSupport.readToString()`
parents 549d31ac fe2c5494
Loading
Loading
Loading
Loading
+8 −4
Original line number Diff line number Diff line
@@ -15,6 +15,8 @@ import java.util.Locale;
import static com.fsck.k9.mail.internet.JisSupport.SHIFT_JIS;

public class CharsetSupport {
    private static final String DEFAULT_CHARSET = "US-ASCII";

    /**
     * Table for character set fall-back.
     *
@@ -23,15 +25,13 @@ public class CharsetSupport {
    private static final String[][] CHARSET_FALLBACK_MAP = new String[][] {
            // Some Android versions don't support KOI8-U
            {"koi8-u", "koi8-r"},
            {"iso-2022-jp-[\\d]+", "iso-2022-jp"},
            // Default fall-back is US-ASCII
            {".*", "US-ASCII"}
            {"iso-2022-jp-[\\d]+", "iso-2022-jp"}
    };


    static String fixupCharset(String charset, Message message) throws MessagingException {
        if (charset == null || "0".equals(charset))
            charset = "US-ASCII";  // No encoding, so use us-ascii, which is the standard.
            charset = DEFAULT_CHARSET;

        charset = charset.toLowerCase(Locale.US);
        if (charset.equals("cp932"))
@@ -93,6 +93,10 @@ public class CharsetSupport {
            }
        }

        if (!supported) {
            charset = DEFAULT_CHARSET;
        }

        /*
         * Convert and return as new String
         */
+24 −0
Original line number Diff line number Diff line
package com.fsck.k9.mail.internet;


import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
@@ -95,4 +99,24 @@ public class CharsetSupportTest {
        expect = "x-kddi-shift_jis-2007";
        assertEquals(expect, CharsetSupport.fixupCharset(charsetOnMail, message));
    }

    @Test
    public void readToString_withUnsupportedCharset_shouldFallBackToAscii() throws IOException {
        InputStream inputStream = new ByteArrayInputStream("input".getBytes());
        String charset = "unsupported";

        String result = CharsetSupport.readToString(inputStream, charset);

        assertEquals("input", result);
    }

    @Test
    public void readToString_withInvalidCharset_shouldFallBackToAscii() throws IOException {
        InputStream inputStream = new ByteArrayInputStream("input".getBytes());
        String charset = "invalid\n";

        String result = CharsetSupport.readToString(inputStream, charset);

        assertEquals("input", result);
    }
}