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

Commit 95b28e90 authored by Siim Sammul's avatar Siim Sammul
Browse files

Move the ParcelFileDescritor initializations in both BootReceiver and

NativeTombstoneManager into the try-catch block so
that they would get closed automatically.

Test: atest ErrorsTest
Bug: 269182937
Change-Id: I659f927ad65b73c81e7881c9a09a6e705e47b98e
parent 077751cd
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -362,10 +362,8 @@ public class BootReceiver extends BroadcastReceiver {
                            PosixFilePermissions.fromString("rw-rw----"));

                    // Write the new proto container proto with headers.
                    ParcelFileDescriptor pfd;
                    try {
                        pfd = ParcelFileDescriptor.open(tombstoneProtoWithHeaders, MODE_READ_WRITE);

                    try (ParcelFileDescriptor pfd = ParcelFileDescriptor.open(
                            tombstoneProtoWithHeaders, MODE_READ_WRITE)) {
                        ProtoOutputStream protoStream = new ProtoOutputStream(
                                pfd.getFileDescriptor());
                        protoStream.write(TombstoneWithHeadersProto.TOMBSTONE, tombstoneBytes);
@@ -379,6 +377,8 @@ public class BootReceiver extends BroadcastReceiver {
                    } catch (FileNotFoundException ex) {
                        Slog.e(TAG, "failed to open for write: " + tombstoneProtoWithHeaders, ex);
                        throw ex;
                    } catch (IOException ex) {
                        Slog.e(TAG, "IO exception during write: " + tombstoneProtoWithHeaders, ex);
                    } finally {
                        // Remove the temporary file.
                        if (tombstoneProtoWithHeaders != null) {
+20 −21
Original line number Diff line number Diff line
@@ -153,17 +153,9 @@ public final class NativeTombstoneManager {
            return Optional.empty();
        }

        ParcelFileDescriptor pfd;
        try {
            pfd = ParcelFileDescriptor.open(path, MODE_READ_WRITE);
        } catch (FileNotFoundException ex) {
            Slog.w(TAG, "failed to open " + path, ex);
            return Optional.empty();
        }

        try (ParcelFileDescriptor pfd = ParcelFileDescriptor.open(path, MODE_READ_WRITE)) {
            final Optional<TombstoneFile> parsedTombstone = TombstoneFile.parse(pfd);
            if (!parsedTombstone.isPresent()) {
            IoUtils.closeQuietly(pfd);
                return Optional.empty();
            }

@@ -179,6 +171,13 @@ public final class NativeTombstoneManager {
            }

            return parsedTombstone;
        } catch (FileNotFoundException ex) {
            Slog.w(TAG, "failed to open " + path, ex);
            return Optional.empty();
        } catch (IOException ex) {
            Slog.e(TAG, "IO exception during write to " + path, ex);
            return Optional.empty();
        }
    }

    /**