Fix bitmap allocation error that causes crash when calling Bitmap::extractAlpha
bug: 19112656
Change-Id: Ib44ba4208449d5873402e9516abc8b6d8fa0b82a
diff --git a/core/jni/android/graphics/Graphics.cpp b/core/jni/android/graphics/Graphics.cpp
index 9996ce1..9ea63aa 100644
--- a/core/jni/android/graphics/Graphics.cpp
+++ b/core/jni/android/graphics/Graphics.cpp
@@ -511,18 +511,15 @@
///////////////////////////////////////////////////////////////////////////////
-static bool computeAllocationSize(const SkImageInfo& info, size_t* size, size_t* rowBytes) {
- int32_t rowBytes32 = SkToS32(info.minRowBytes());
- int64_t bigSize = (int64_t)info.height() * rowBytes32;
- if (rowBytes32 < 0 || !sk_64_isS32(bigSize)) {
- return false; // allocation will be too large
- }
+static bool computeAllocationSize(const SkBitmap& bitmap, size_t* size) {
+ int32_t rowBytes32 = SkToS32(bitmap.rowBytes());
+ int64_t bigSize = (int64_t)bitmap.height() * rowBytes32;
+ if (rowBytes32 < 0 || !sk_64_isS32(bigSize)) {
+ return false; // allocation will be too large
+ }
- *size = sk_64_asS32(bigSize);
- *rowBytes = rowBytes32;
-
- SkASSERT(*size >= info.getSafeSize(*rowBytes));
- return true;
+ *size = sk_64_asS32(bigSize);
+ return true;
}
jbyteArray GraphicsJNI::allocateJavaPixelRef(JNIEnv* env, SkBitmap* bitmap,
@@ -533,11 +530,15 @@
return NULL;
}
- size_t size, rowBytes;
- if (!computeAllocationSize(info, &size, &rowBytes)) {
+ size_t size;
+ if (!computeAllocationSize(*bitmap, &size)) {
return NULL;
}
+ // we must respect the rowBytes value already set on the bitmap instead of
+ // attempting to compute our own.
+ const size_t rowBytes = bitmap->rowBytes();
+
jbyteArray arrayObj = (jbyteArray) env->CallObjectMethod(gVMRuntime,
gVMRuntime_newNonMovableArray,
gByte_class, size);
@@ -580,11 +581,15 @@
return NULL;
}
- size_t size, rowBytes;
- if (!computeAllocationSize(info, &size, &rowBytes)) {
+ size_t size;
+ if (!computeAllocationSize(*bitmap, &size)) {
return false;
}
+ // we must respect the rowBytes value already set on the bitmap instead of
+ // attempting to compute our own.
+ const size_t rowBytes = bitmap->rowBytes();
+
void* addr = sk_malloc_flags(size, 0);
if (NULL == addr) {
return false;