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

Commit f0d3f371 authored by Tobias Preuss's avatar Tobias Preuss
Browse files

Avoid leaking CountingOutputStream.

+ Affects com.fsck.k9.mail.filter.CountingOutputStream.
parent 2db95470
Loading
Loading
Loading
Loading
+6 −11
Original line number Diff line number Diff line
@@ -785,21 +785,16 @@ public class LocalFolder {
    }

    private long decodeAndCountBytes(InputStream rawInputStream, String encoding, long fallbackValue) {
        InputStream decodingInputStream = localStore.getDecodingInputStream(rawInputStream, encoding);
        try {
            CountingOutputStream countingOutputStream = new CountingOutputStream();
            try {
                IOUtils.copy(decodingInputStream, countingOutputStream);
        try (InputStream decodingInputStream = localStore.getDecodingInputStream(rawInputStream, encoding)) {

            try (CountingOutputStream countingOutputStream = new CountingOutputStream()) {
                IOUtils.copy(decodingInputStream, countingOutputStream);
                return countingOutputStream.getCount();
            }

        } catch (IOException e) {
            return fallbackValue;
        }
        } finally {
            try {
                decodingInputStream.close();
            } catch (IOException e) { /* ignore */ }
        }
    }

    private byte[] getHeaderBytes(Part part) throws IOException, MessagingException {
+1 −2
Original line number Diff line number Diff line
@@ -99,8 +99,7 @@ public class RawMessageProvider extends ContentProvider {

    private long computeMessageSize(LocalMessage message) {
        // TODO: Store message size in database when saving message so this can be a simple lookup instead.
        try {
            CountingOutputStream countingOutputStream = new CountingOutputStream();
        try (CountingOutputStream countingOutputStream = new CountingOutputStream()) {
            message.writeTo(countingOutputStream);
            return countingOutputStream.getCount();
        } catch (IOException | MessagingException e) {
+4 −3
Original line number Diff line number Diff line
@@ -305,10 +305,11 @@ internal class SaveMessageOperations(
    private fun decodeAndCountBytes(rawInputStream: InputStream, encoding: String, fallbackValue: Long): Long {
        return try {
            getDecodingInputStream(rawInputStream, encoding).use { decodingInputStream ->
                val countingOutputStream = CountingOutputStream()
                CountingOutputStream().use { countingOutputStream ->
                    IOUtils.copy(decodingInputStream, countingOutputStream)
                    countingOutputStream.count
                }
            }
        } catch (e: IOException) {
            fallbackValue
        }
+2 −6
Original line number Diff line number Diff line
@@ -159,16 +159,12 @@ public abstract class Message implements Part, Body {
    public abstract void setEncoding(String encoding) throws MessagingException;

    public long calculateSize() {
        try {

            CountingOutputStream out = new CountingOutputStream();
        try (CountingOutputStream out = new CountingOutputStream()) {
            EOLConvertingOutputStream eolOut = new EOLConvertingOutputStream(out);
            writeTo(eolOut);
            eolOut.flush();
            return out.getCount();
        } catch (IOException e) {
            Timber.e(e, "Failed to calculate a message size");
        } catch (MessagingException e) {
        } catch (IOException | MessagingException e) {
            Timber.e(e, "Failed to calculate a message size");
        }
        return 0;
+7 −12
Original line number Diff line number Diff line
@@ -113,23 +113,18 @@ public class TextBody implements Body, SizeAware {
    }

    private long getLengthWhenQuotedPrintableEncoded(byte[] bytes) throws IOException {
        CountingOutputStream countingOutputStream = new CountingOutputStream();
        try (CountingOutputStream countingOutputStream = new CountingOutputStream()) {
            writeSignSafeQuotedPrintable(countingOutputStream, bytes);
            return countingOutputStream.getCount();
        }
    }

    private void writeSignSafeQuotedPrintable(OutputStream out, byte[] bytes) throws IOException {
        SignSafeOutputStream signSafeOutputStream = new SignSafeOutputStream(out);
        try {
            QuotedPrintableOutputStream signSafeQuotedPrintableOutputStream =
                    new QuotedPrintableOutputStream(signSafeOutputStream, false);
            try {
        try (SignSafeOutputStream signSafeOutputStream = new SignSafeOutputStream(out)) {
            try (QuotedPrintableOutputStream signSafeQuotedPrintableOutputStream = new QuotedPrintableOutputStream(
                    signSafeOutputStream, false)) {
                signSafeQuotedPrintableOutputStream.write(bytes);
            } finally {
                signSafeQuotedPrintableOutputStream.close();
            }
        } finally {
            signSafeOutputStream.close();
        }
    }