Merge "Fix some TextLayoutCache issues"
diff --git a/core/jni/android/graphics/Paint.cpp b/core/jni/android/graphics/Paint.cpp
index 423eff4..7d222f6 100644
--- a/core/jni/android/graphics/Paint.cpp
+++ b/core/jni/android/graphics/Paint.cpp
@@ -366,26 +366,22 @@
         NPE_CHECK_RETURN_ZERO(env, jpaint);
         NPE_CHECK_RETURN_ZERO(env, text);
 
+        size_t textLength = env->GetStringLength(text);
         int count = end - start;
-        if ((start | count) < 0) {
+        if ((start | count) < 0 || (size_t)end > textLength) {
             doThrowAIOOBE(env);
             return 0;
         }
         if (count == 0) {
             return 0;
         }
-        size_t textLength = env->GetStringLength(text);
-        if ((size_t)count > textLength) {
-            doThrowAIOOBE(env);
-            return 0;
-        }
 
         const jchar* textArray = env->GetStringChars(text, NULL);
         SkPaint* paint = GraphicsJNI::getNativePaint(env, jpaint);
         jfloat width = 0;
 
 #if RTL_USE_HARFBUZZ
-        TextLayout::getTextRunAdvances(paint, textArray, start, count, end,
+        TextLayout::getTextRunAdvances(paint, textArray, start, count, textLength,
                 paint->getFlags(), NULL /* dont need all advances */, width);
 #else
 
diff --git a/core/jni/android/graphics/TextLayoutCache.cpp b/core/jni/android/graphics/TextLayoutCache.cpp
index 7f79277..d04e059 100644
--- a/core/jni/android/graphics/TextLayoutCache.cpp
+++ b/core/jni/android/graphics/TextLayoutCache.cpp
@@ -163,20 +163,20 @@
                 // Update timing information for statistics
                 value->setElapsedTime(endTime - startTime);
 
-                LOGD("CACHE MISS: Added entry for text='%s' with start=%d, count=%d, "
+                LOGD("CACHE MISS: Added entry with start=%d, count=%d, "
                         "contextCount=%d, entry size %d bytes, remaining space %d bytes"
-                        " - Compute time in nanos: %d",
-                        String8(text, contextCount).string(), start, count, contextCount,
-                        size, mMaxSize - mSize, value->getElapsedTime());
+                        " - Compute time in nanos: %d - Text='%s' ",
+                        start, count, contextCount, size, mMaxSize - mSize, value->getElapsedTime(),
+                        String8(text, contextCount).string());
             }
         } else {
             if (mDebugEnabled) {
                 LOGD("CACHE MISS: Calculated but not storing entry because it is too big "
-                        "for text='%s' with start=%d, count=%d, contextCount=%d, "
+                        "with start=%d, count=%d, contextCount=%d, "
                         "entry size %d bytes, remaining space %d bytes"
-                        " - Compute time in nanos: %lld",
-                        String8(text, contextCount).string(), start, count, contextCount,
-                        size, mMaxSize - mSize, endTime);
+                        " - Compute time in nanos: %lld - Text='%s'",
+                        start, count, contextCount, size, mMaxSize - mSize, endTime,
+                        String8(text, contextCount).string());
             }
             value.clear();
         }
@@ -190,12 +190,12 @@
             if (value->getElapsedTime() > 0) {
                 float deltaPercent = 100 * ((value->getElapsedTime() - elapsedTimeThruCacheGet)
                         / ((float)value->getElapsedTime()));
-                LOGD("CACHE HIT #%d for text='%s' with start=%d, count=%d, contextCount=%d "
+                LOGD("CACHE HIT #%d with start=%d, count=%d, contextCount=%d "
                         "- Compute time in nanos: %d - "
-                        "Cache get time in nanos: %lld - Gain in percent: %2.2f",
-                        mCacheHitCount, String8(text, contextCount).string(), start, count,
-                        contextCount,
-                        value->getElapsedTime(), elapsedTimeThruCacheGet, deltaPercent);
+                        "Cache get time in nanos: %lld - Gain in percent: %2.2f - Text='%s' ",
+                        mCacheHitCount, start, count, contextCount,
+                        value->getElapsedTime(), elapsedTimeThruCacheGet, deltaPercent,
+                        String8(text, contextCount).string());
             }
             if (mCacheHitCount % DEFAULT_DUMP_STATS_CACHE_HIT_INTERVAL == 0) {
                 dumpCacheStats();