@@ -191,6 +191,8 @@ CHIP_ERROR ChipDnssdBrowse(const char * type, DnssdServiceProtocol protocol, Ine
191
191
192
192
std::string serviceType = GetFullTypeWithSubTypes (type, protocol);
193
193
JNIEnv * env = JniReferences::GetInstance ().GetEnvForCurrentThread ();
194
+ VerifyOrReturnError (env != nullptr , CHIP_JNI_ERROR_NO_ENV,
195
+ ChipLogError (Discovery, " Failed to GetEnvForCurrentThread for ChipDnssdBrowse" ));
194
196
UtfString jniServiceType (env, serviceType.c_str ());
195
197
196
198
env->CallVoidMethod (sBrowserObject .ObjectRef (), sBrowseMethod , jniServiceType.jniValue (), reinterpret_cast <jlong>(callback),
@@ -204,22 +206,29 @@ CHIP_ERROR ChipDnssdBrowse(const char * type, DnssdServiceProtocol protocol, Ine
204
206
return CHIP_JNI_ERROR_EXCEPTION_THROWN;
205
207
}
206
208
207
- auto sdCtx = chip::Platform::New<BrowseContext>(callback);
209
+ auto sdCtx = chip::Platform::New<BrowseContext>(callback);
210
+ VerifyOrReturnError (nullptr != sdCtx, CHIP_ERROR_NO_MEMORY,
211
+ ChipLogError (Discovery, " Failed allocate memory for BrowseContext in ChipDnssdBrowse" ));
208
212
*browseIdentifier = reinterpret_cast <intptr_t >(sdCtx);
209
213
210
214
return CHIP_NO_ERROR;
211
215
}
212
216
213
217
CHIP_ERROR ChipDnssdStopBrowse (intptr_t browseIdentifier)
214
218
{
219
+ VerifyOrReturnError (browseIdentifier != 0 , CHIP_ERROR_INVALID_ARGUMENT,
220
+ ChipLogError (Discovery, " ChipDnssdStopBrowse Invalid argument browseIdentifier = 0" ));
215
221
VerifyOrReturnError (sBrowserObject .HasValidObjectRef () && sStopBrowseMethod != nullptr , CHIP_ERROR_INVALID_ARGUMENT);
216
222
217
223
JNIEnv * env = JniReferences::GetInstance ().GetEnvForCurrentThread ();
218
- auto ctx = reinterpret_cast <BrowseContext *>(browseIdentifier);
224
+ VerifyOrReturnError (env != nullptr , CHIP_JNI_ERROR_NO_ENV,
225
+ ChipLogError (Discovery, " Failed to GetEnvForCurrentThread for ChipDnssdStopBrowse" ));
226
+ auto ctx = reinterpret_cast <BrowseContext *>(browseIdentifier);
219
227
220
228
env->CallVoidMethod (sBrowserObject .ObjectRef (), sStopBrowseMethod , reinterpret_cast <jlong>(ctx->callback ));
221
229
222
230
chip::Platform::Delete (ctx);
231
+ ctx = nullptr ;
223
232
if (env->ExceptionCheck ())
224
233
{
225
234
ChipLogError (Discovery, " Java exception in ChipDnssdStopBrowse" );
@@ -339,6 +348,12 @@ void InitializeWithObjects(jobject resolverObject, jobject browserObject, jobjec
339
348
env->ExceptionClear ();
340
349
}
341
350
351
+ if (sStopBrowseMethod == nullptr )
352
+ {
353
+ ChipLogError (Discovery, " Failed to access Discover 'stopDiscover' method" );
354
+ env->ExceptionClear ();
355
+ }
356
+
342
357
if (sGetTextEntryKeysMethod == nullptr )
343
358
{
344
359
ChipLogError (Discovery, " Failed to access MdnsCallback 'getTextEntryKeys' method" );
0 commit comments