Skip to content

Commit

Permalink
Fix potential memory leak when attestation delegate is set (#24906)
Browse files Browse the repository at this point in the history
  • Loading branch information
yufengwangca authored and pull[bot] committed Aug 17, 2023
1 parent 4c3567f commit 1103184
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/controller/java/DeviceAttestationDelegateBridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ void DeviceAttestationDelegateBridge::OnDeviceAttestationCompleted(
VerifyOrReturn(deviceAttestationDelegateCls != nullptr,
ChipLogError(Controller, "Could not find device attestation delegate class."));

// Auto delete deviceAttestationDelegateCls object when exit from the local scope
JniClass deviceAttestationDelegateJniCls(deviceAttestationDelegateCls);

if (env->IsInstanceOf(mDeviceAttestationDelegate, deviceAttestationDelegateCls))
{
jmethodID onDeviceAttestationCompletedMethod;
Expand All @@ -87,10 +90,17 @@ void DeviceAttestationDelegateBridge::OnDeviceAttestationCompleted(
&onDeviceAttestationCompletedMethod);
VerifyOrReturn(onDeviceAttestationCompletedMethod != nullptr,
ChipLogError(Controller, "Could not find deviceAttestation completed method"));
jobject javaAttestationInfo;
CHIP_ERROR err = N2J_AttestationInfo(env, info, javaAttestationInfo);
VerifyOrReturn(err == CHIP_NO_ERROR,
ChipLogError(Controller, "Failed to create AttestationInfo, error: %s", err.AsString()));

jobject javaAttestationInfo = nullptr;

// Don't need to pass attestationInfo for additional verification when attestation failed.
if (attestationResult == chip::Credentials::AttestationVerificationResult::kSuccess)
{
CHIP_ERROR err = N2J_AttestationInfo(env, info, javaAttestationInfo);
VerifyOrReturn(err == CHIP_NO_ERROR,
ChipLogError(Controller, "Failed to create AttestationInfo, error: %s", err.AsString()));
}

env->CallVoidMethod(mDeviceAttestationDelegate, onDeviceAttestationCompletedMethod, reinterpret_cast<jlong>(device),
javaAttestationInfo, static_cast<jint>(attestationResult));
}
Expand Down

0 comments on commit 1103184

Please sign in to comment.