OSDN Git Service

Parcel: Avoid NPE in finalization
authorAndreas Gampe <agampe@google.com>
Tue, 10 Oct 2017 15:01:38 +0000 (08:01 -0700)
committerAndreas Gampe <agampe@google.com>
Tue, 10 Oct 2017 15:06:42 +0000 (08:06 -0700)
Check whether the guard is null to avoid:

 Uncaught exception thrown by finalizer
 java.lang.NullPointerException: Attempt to invoke virtual method 'void dalvik.system.CloseGuard.close()' on a null object reference
      at android.os.ParcelFileDescriptor.closeWithStatus(ParcelFileDescriptor.java:740)
      at android.os.ParcelFileDescriptor.finalize(ParcelFileDescriptor.java:990)
      at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:250)
      at java.lang.Daemons$FinalizerDaemon.runInternal(Daemons.java:237)
      at java.lang.Daemons$Daemon.run(Daemons.java:103)
      at java.lang.Thread.run(Thread.java:764)

Follow-up to commit da5a3e12f4f8f965c57d6f93c74190f43ea233f3.

Bug: 7426029
Bug: 10330121
Test: m
Change-Id: I903f1545ab784008727ff23bb95fe182bd95b62a

core/java/android/os/ParcelFileDescriptor.java

index c091420..7f588ad 100644 (file)
@@ -737,7 +737,9 @@ public class ParcelFileDescriptor implements Parcelable, Closeable {
     private void closeWithStatus(int status, String msg) {
         if (mClosed) return;
         mClosed = true;
-        mGuard.close();
+        if (mGuard != null) {
+            mGuard.close();
+        }
         // Status MUST be sent before closing actual descriptor
         writeCommStatusAndClose(status, msg);
         IoUtils.closeQuietly(mFd);