Bug 803613 - Guard against NPE by synchronizing the destroy function and checking for null. r=blassey
authorKartikaya Gupta <kgupta@mozilla.com>
Mon, 22 Oct 2012 10:14:39 -0400
changeset 111161 e4fa894b7348ffcc43bd3373f0f04cf03c075bd9
parent 111160 92f1c0fad249dc8aeb0efe3c2f9f550e2be910ed
child 111162 666a61b83149deb6aa89198e1f1098871b9e925c
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewersblassey
bugs803613
milestone19.0a1
Bug 803613 - Guard against NPE by synchronizing the destroy function and checking for null. r=blassey
mobile/android/base/gfx/ScreenshotLayer.java
--- a/mobile/android/base/gfx/ScreenshotLayer.java
+++ b/mobile/android/base/gfx/ScreenshotLayer.java
@@ -111,47 +111,53 @@ public class ScreenshotLayer extends Sin
                 DirectBufferAllocator.free(mBuffer);
                 mBuffer = null;
             } finally {
                 super.finalize();
             }
         }
 
         @Override
-        public void destroy() {
+        public synchronized void destroy() {
             try {
                 DirectBufferAllocator.free(mBuffer);
                 mBuffer = null;
             } catch (Exception ex) {
                 Log.e(LOGTAG, "error clearing buffers: ", ex);
             }
         }
 
-        void copyBuffer(ByteBuffer src, ByteBuffer dst, Rect rect, int stride) {
+        private void copyBuffer(ByteBuffer src, ByteBuffer dst, Rect rect, int stride) {
             int start = (rect.top * stride) + (rect.left * BYTES_FOR_16BPP);
             int end = ((rect.bottom - 1) * stride) + (rect.right * BYTES_FOR_16BPP);
             // clamp stuff just to be safe
             start = Math.max(0, Math.min(dst.limit(), Math.min(src.limit(), start)));
             end = Math.max(start, Math.min(dst.limit(), Math.min(src.capacity(), end)));
             dst.position(start);
             src.position(start).limit(end);
             // This allocates a lot of memory and can fail sometimes. Handling the
             // exception is better than crashing.
             try {
               dst.put(src);
             } catch (java.lang.OutOfMemoryError e) {}
         }
 
         synchronized void setBitmap(ByteBuffer data, int width, int height, int format, Rect rect) {
+            if (mBuffer == null) {
+                return;
+            }
             mSize = new IntSize(width, height);
             mFormat = format;
             copyBuffer(data.asReadOnlyBuffer(), mBuffer.duplicate(), rect, width * BYTES_FOR_16BPP);
         }
 
         synchronized void setBitmap(Bitmap bitmap, int width, int height, int format) throws IllegalArgumentException {
+            if (mBuffer == null) {
+                return;
+            }
             Bitmap tmp;
             mSize = new IntSize(width, height);
             mFormat = format;
             if (width == bitmap.getWidth() && height == bitmap.getHeight()) {
                 tmp = bitmap;
                 tmp.copyPixelsToBuffer(mBuffer.asIntBuffer());
             } else {
                 throw new IllegalArgumentException("### unexpected size in setBitmap: w="+width+" h="+height);