Skip to content

Added error checking in Android DnssdImpl.cpp and fixed examples/tv-casting-app bug #33010

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ public void onResume() {
public void onPause() {
super.onPause();
Log.i(TAG, "onPause() called");
stopDiscovery();
}

/** Interface for notifying the host. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ JNI_METHOD(jobject, removeCastingPlayerChangeListener)(JNIEnv * env, jobject, jo

return support::convertMatterErrorFromCppToJava(CHIP_NO_ERROR);
}
else if (DiscoveryDelegateImpl::GetInstance()->castingPlayerChangeListenerJavaObject.ObjectRef() == nullptr)
else if (!DiscoveryDelegateImpl::GetInstance()->castingPlayerChangeListenerJavaObject.HasValidObjectRef())
{
ChipLogError(
AppServer,
Expand Down
19 changes: 17 additions & 2 deletions src/platform/android/DnssdImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ CHIP_ERROR ChipDnssdBrowse(const char * type, DnssdServiceProtocol protocol, Ine

std::string serviceType = GetFullTypeWithSubTypes(type, protocol);
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
VerifyOrReturnError(env != nullptr, CHIP_JNI_ERROR_NO_ENV,
ChipLogError(Discovery, "Failed to GetEnvForCurrentThread for ChipDnssdBrowse"));
UtfString jniServiceType(env, serviceType.c_str());

env->CallVoidMethod(sBrowserObject.ObjectRef(), sBrowseMethod, jniServiceType.jniValue(), reinterpret_cast<jlong>(callback),
Expand All @@ -204,22 +206,29 @@ CHIP_ERROR ChipDnssdBrowse(const char * type, DnssdServiceProtocol protocol, Ine
return CHIP_JNI_ERROR_EXCEPTION_THROWN;
}

auto sdCtx = chip::Platform::New<BrowseContext>(callback);
auto sdCtx = chip::Platform::New<BrowseContext>(callback);
VerifyOrReturnError(nullptr != sdCtx, CHIP_ERROR_NO_MEMORY,
ChipLogError(Discovery, "Failed allocate memory for BrowseContext in ChipDnssdBrowse"));
*browseIdentifier = reinterpret_cast<intptr_t>(sdCtx);

return CHIP_NO_ERROR;
}

CHIP_ERROR ChipDnssdStopBrowse(intptr_t browseIdentifier)
{
VerifyOrReturnError(browseIdentifier != 0, CHIP_ERROR_INVALID_ARGUMENT,
ChipLogError(Discovery, "ChipDnssdStopBrowse Invalid argument browseIdentifier = 0"));
VerifyOrReturnError(sBrowserObject.HasValidObjectRef() && sStopBrowseMethod != nullptr, CHIP_ERROR_INVALID_ARGUMENT);

JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
auto ctx = reinterpret_cast<BrowseContext *>(browseIdentifier);
VerifyOrReturnError(env != nullptr, CHIP_JNI_ERROR_NO_ENV,
ChipLogError(Discovery, "Failed to GetEnvForCurrentThread for ChipDnssdStopBrowse"));
auto ctx = reinterpret_cast<BrowseContext *>(browseIdentifier);

env->CallVoidMethod(sBrowserObject.ObjectRef(), sStopBrowseMethod, reinterpret_cast<jlong>(ctx->callback));

chip::Platform::Delete(ctx);
ctx = nullptr;
if (env->ExceptionCheck())
{
ChipLogError(Discovery, "Java exception in ChipDnssdStopBrowse");
Expand Down Expand Up @@ -339,6 +348,12 @@ void InitializeWithObjects(jobject resolverObject, jobject browserObject, jobjec
env->ExceptionClear();
}

if (sStopBrowseMethod == nullptr)
{
ChipLogError(Discovery, "Failed to access Discover 'stopDiscover' method");
env->ExceptionClear();
}

if (sGetTextEntryKeysMethod == nullptr)
{
ChipLogError(Discovery, "Failed to access MdnsCallback 'getTextEntryKeys' method");
Expand Down
Loading