Merge cherrypicks of [4314173, 4314174, 4314175, 4314176, 4314471, 4314472, 4314473, 4314474, 4314475, 4314645, 4314646, 4314193, 4314476, 4314477, 4315350, 4315351, 4315352, 4315353, 4315354, 4314478, 4315430, 4314194, 4314195, 4314196, 4314197, 4314198, 4314199, 4314200, 4315093, 4315094, 4315095, 4315096, 4315097, 4315098, 4315099, 4315100, 4315501, 4315502, 4315503, 4314177, 4315431, 4315432, 4315433, 4315434, 4314178, 4314179, 4315355, 4315435, 4315382, 4315403, 4315404, 4315436, 4315437, 4315438, 4315439, 4315440, 4315521, 4315522, 4315523, 4315524, 4315525, 4315526, 4315527, 4315528, 4315529, 4314035, 4314230, 4315356, 4315530, 4315531, 4315471, 4315357, 4315358, 4314479, 4315532, 4315533, 4315534, 4315535, 4315536] into sparse-4732991-L06700000181398573

Change-Id: I62c082b2907586c63940c30f3d49756686f6d7bf
diff --git a/jni/com_android_bluetooth_gatt.cpp b/jni/com_android_bluetooth_gatt.cpp
index 3bfbcfd..e57a1d0 100644
--- a/jni/com_android_bluetooth_gatt.cpp
+++ b/jni/com_android_bluetooth_gatt.cpp
@@ -1650,7 +1650,13 @@
   response.attr_value.len = 0;
 
   if (val != NULL) {
-    response.attr_value.len = (uint16_t)env->GetArrayLength(val);
+    if (env->GetArrayLength(val) < BTGATT_MAX_ATTR_LEN) {
+      response.attr_value.len = (uint16_t)env->GetArrayLength(val);
+    } else {
+      android_errorWriteLog(0x534e4554, "78787521");
+      response.attr_value.len = BTGATT_MAX_ATTR_LEN;
+    }
+
     jbyte* array = env->GetByteArrayElements(val, 0);
 
     for (int i = 0; i != response.attr_value.len; ++i)