Merge "Prevent usage of freed memory by using getLights and getSensors" into sc-dev

This commit is contained in:
TreeHugger Robot
2021-06-09 02:40:20 +00:00
committed by Android (Google) Code Review

View File

@@ -1987,43 +1987,39 @@ static jobject nativeGetLights(JNIEnv* env, jclass clazz, jlong ptr, jint device
NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr); NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
jobject jLights = env->NewObject(gArrayListClassInfo.clazz, gArrayListClassInfo.constructor); jobject jLights = env->NewObject(gArrayListClassInfo.clazz, gArrayListClassInfo.constructor);
std::vector<int> lightIds = im->getInputManager()->getReader()->getLightIds(deviceId); std::vector<InputDeviceLightInfo> lights =
im->getInputManager()->getReader()->getLights(deviceId);
for (size_t i = 0; i < lightIds.size(); i++) { for (size_t i = 0; i < lights.size(); i++) {
const InputDeviceLightInfo* lightInfo = const InputDeviceLightInfo& lightInfo = lights[i];
im->getInputManager()->getReader()->getLightInfo(deviceId, lightIds[i]);
if (lightInfo == nullptr) {
ALOGW("Failed to get input device %d light info for id %d", deviceId, lightIds[i]);
continue;
}
jint jTypeId = jint jTypeId =
env->GetStaticIntField(gLightClassInfo.clazz, gLightClassInfo.lightTypeInput); env->GetStaticIntField(gLightClassInfo.clazz, gLightClassInfo.lightTypeInput);
jint jCapability = 0; jint jCapability = 0;
if (lightInfo->type == InputDeviceLightType::MONO) { if (lightInfo.type == InputDeviceLightType::MONO) {
jCapability = env->GetStaticIntField(gLightClassInfo.clazz, jCapability = env->GetStaticIntField(gLightClassInfo.clazz,
gLightClassInfo.lightCapabilityBrightness); gLightClassInfo.lightCapabilityBrightness);
} else if (lightInfo->type == InputDeviceLightType::RGB || } else if (lightInfo.type == InputDeviceLightType::RGB ||
lightInfo->type == InputDeviceLightType::MULTI_COLOR) { lightInfo.type == InputDeviceLightType::MULTI_COLOR) {
jCapability = jCapability =
env->GetStaticIntField(gLightClassInfo.clazz, env->GetStaticIntField(gLightClassInfo.clazz,
gLightClassInfo.lightCapabilityBrightness) | gLightClassInfo.lightCapabilityBrightness) |
env->GetStaticIntField(gLightClassInfo.clazz, env->GetStaticIntField(gLightClassInfo.clazz,
gLightClassInfo.lightCapabilityRgb); gLightClassInfo.lightCapabilityRgb);
} else if (lightInfo->type == InputDeviceLightType::PLAYER_ID) { } else if (lightInfo.type == InputDeviceLightType::PLAYER_ID) {
jTypeId = env->GetStaticIntField(gLightClassInfo.clazz, jTypeId = env->GetStaticIntField(gLightClassInfo.clazz,
gLightClassInfo.lightTypePlayerId); gLightClassInfo.lightTypePlayerId);
} else { } else {
ALOGW("Unknown light type %d", lightInfo->type); ALOGW("Unknown light type %d", lightInfo.type);
continue; continue;
} }
ScopedLocalRef<jobject> lightObj(env, ScopedLocalRef<jobject> lightObj(env,
env->NewObject(gLightClassInfo.clazz, env->NewObject(gLightClassInfo.clazz,
gLightClassInfo.constructor, gLightClassInfo.constructor,
static_cast<jint>(lightInfo->id), static_cast<jint>(lightInfo.id),
env->NewStringUTF(lightInfo->name.c_str()), env->NewStringUTF(lightInfo.name.c_str()),
static_cast<jint>(lightInfo->ordinal), static_cast<jint>(lightInfo.ordinal),
jTypeId, jCapability)); jTypeId, jCapability));
// Add light object to list // Add light object to list
env->CallBooleanMethod(jLights, gArrayListClassInfo.add, lightObj.get()); env->CallBooleanMethod(jLights, gArrayListClassInfo.add, lightObj.get());
@@ -2218,39 +2214,28 @@ static jobject createInputSensorInfo(JNIEnv* env, jstring name, jstring vendor,
static jobjectArray nativeGetSensorList(JNIEnv* env, jclass /* clazz */, jlong ptr, jint deviceId) { static jobjectArray nativeGetSensorList(JNIEnv* env, jclass /* clazz */, jlong ptr, jint deviceId) {
NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr); NativeInputManager* im = reinterpret_cast<NativeInputManager*>(ptr);
std::vector<InputDeviceInfo> devices = im->getInputManager()->getReader()->getInputDevices(); std::vector<InputDeviceSensorInfo> sensors =
// Find the input device by deviceId im->getInputManager()->getReader()->getSensors(deviceId);
auto it = std::find_if(devices.begin(), devices.end(),
[deviceId](InputDeviceInfo& info) { return info.getId() == deviceId; });
if (it == devices.end()) { jobjectArray arr = env->NewObjectArray(sensors.size(), gInputSensorInfo.clazz, nullptr);
// Return an array of size 0 for (int i = 0; i < sensors.size(); i++) {
return env->NewObjectArray(0, gInputSensorInfo.clazz, nullptr); const InputDeviceSensorInfo& sensorInfo = sensors[i];
}
std::vector<InputDeviceSensorType> types = it->getSensorTypes(); jobject info = createInputSensorInfo(env, env->NewStringUTF(sensorInfo.name.c_str()),
jobjectArray arr = env->NewObjectArray(types.size(), gInputSensorInfo.clazz, nullptr); env->NewStringUTF(sensorInfo.vendor.c_str()),
for (int i = 0; i < types.size(); i++) { static_cast<jint>(sensorInfo.version), 0 /* handle */,
const InputDeviceSensorInfo* sensorInfo = it->getSensorInfo(types[i]); static_cast<jint>(sensorInfo.type),
if (sensorInfo == nullptr) { static_cast<jfloat>(sensorInfo.maxRange),
ALOGW("Failed to get input device %d sensor info for type %s", deviceId, static_cast<jfloat>(sensorInfo.resolution),
NamedEnum::string(types[i]).c_str()); static_cast<jfloat>(sensorInfo.power),
continue; static_cast<jfloat>(sensorInfo.minDelay),
} static_cast<jint>(sensorInfo.fifoReservedEventCount),
static_cast<jint>(sensorInfo.fifoMaxEventCount),
jobject info = env->NewStringUTF(sensorInfo.stringType.c_str()),
createInputSensorInfo(env, env->NewStringUTF(sensorInfo->name.c_str()),
env->NewStringUTF(sensorInfo->vendor.c_str()),
(jint)sensorInfo->version, 0 /* handle */,
(jint)sensorInfo->type, (jfloat)sensorInfo->maxRange,
(jfloat)sensorInfo->resolution, (jfloat)sensorInfo->power,
(jfloat)sensorInfo->minDelay,
(jint)sensorInfo->fifoReservedEventCount,
(jint)sensorInfo->fifoMaxEventCount,
env->NewStringUTF(sensorInfo->stringType.c_str()),
env->NewStringUTF("") /* requiredPermission */, env->NewStringUTF("") /* requiredPermission */,
(jint)sensorInfo->maxDelay, (jint)sensorInfo->flags, static_cast<jint>(sensorInfo.maxDelay),
(jint)sensorInfo->id); static_cast<jint>(sensorInfo.flags),
static_cast<jint>(sensorInfo.id));
env->SetObjectArrayElement(arr, i, info); env->SetObjectArrayElement(arr, i, info);
env->DeleteLocalRef(info); env->DeleteLocalRef(info);
} }