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

Commit 492e9e85 authored by Narayan Kamath's avatar Narayan Kamath
Browse files

Properly guard access to CloseGuard in finalizers.

CloseGuard instances are allocated in constructors and usually
assigned to final fields. This implies they're non-null in finalizers
except in the case where the constructor throws. We add a null check
to make sure we can continue cleaning up other state in the finalizer
(if applicable).

Also, this change decouples closeguard warnings in constructors
from other state based logic. This because the logic there is usually
duplicated with the call to close().

NOTE: This change is not a "complete" fix. Many of these finalizers
are broken in the case where <init> throws. The only objective of
this change is to make such errors more obvious.

Note that some of these classes don't have CTS tests.

Test: make, CtsMediaTestCases.
Bug: 35609098

Change-Id: I24d9e0215f80e44914dba8ab99b6312fd6ed1fc0
parent 2a071d69
Loading
Loading
Loading
Loading
+8 −2
Original line number Original line Diff line number Diff line
@@ -524,7 +524,10 @@ public class ContentProviderClient implements AutoCloseable {
    @Override
    @Override
    protected void finalize() throws Throwable {
    protected void finalize() throws Throwable {
        try {
        try {
            if (mCloseGuard != null) {
                mCloseGuard.warnIfOpen();
                mCloseGuard.warnIfOpen();
            }

            close();
            close();
        } finally {
        } finally {
            super.finalize();
            super.finalize();
@@ -579,7 +582,10 @@ public class ContentProviderClient implements AutoCloseable {
        @Override
        @Override
        protected void finalize() throws Throwable {
        protected void finalize() throws Throwable {
            try {
            try {
                if (mCloseGuard != null) {
                    mCloseGuard.warnIfOpen();
                    mCloseGuard.warnIfOpen();
                }

                close();
                close();
            } finally {
            } finally {
                super.finalize();
                super.finalize();
+4 −1
Original line number Original line Diff line number Diff line
@@ -2942,7 +2942,10 @@ public abstract class ContentResolver {
        @Override
        @Override
        protected void finalize() throws Throwable {
        protected void finalize() throws Throwable {
            try {
            try {
                if (mCloseGuard != null) {
                    mCloseGuard.warnIfOpen();
                    mCloseGuard.warnIfOpen();
                }

                close();
                close();
            } finally {
            } finally {
                super.finalize();
                super.finalize();
+4 −1
Original line number Original line Diff line number Diff line
@@ -152,7 +152,10 @@ public final class SensorDirectChannel implements AutoCloseable {
    @Override
    @Override
    protected void finalize() throws Throwable {
    protected void finalize() throws Throwable {
        try {
        try {
            if (mCloseGuard != null) {
                mCloseGuard.warnIfOpen();
                mCloseGuard.warnIfOpen();
            }

            close();
            close();
        } finally {
        } finally {
            super.finalize();
            super.finalize();
+3 −1
Original line number Original line Diff line number Diff line
@@ -332,7 +332,9 @@ public class UsbDeviceConnection {
    @Override
    @Override
    protected void finalize() throws Throwable {
    protected void finalize() throws Throwable {
        try {
        try {
            if (mCloseGuard != null) {
                mCloseGuard.warnIfOpen();
                mCloseGuard.warnIfOpen();
            }
        } finally {
        } finally {
            super.finalize();
            super.finalize();
        }
        }
+4 −1
Original line number Original line Diff line number Diff line
@@ -114,7 +114,10 @@ public class UsbRequest {
    @Override
    @Override
    protected void finalize() throws Throwable {
    protected void finalize() throws Throwable {
        try {
        try {
            if (mCloseGuard != null) {
                mCloseGuard.warnIfOpen();
                mCloseGuard.warnIfOpen();
            }

            close();
            close();
        } finally {
        } finally {
            super.finalize();
            super.finalize();
Loading