Skip to content

Commit 3828186

Browse files
authored
Delete local JNI references in SAF VFS implementation (#18503)
1 parent 80241cf commit 3828186

2 files changed

Lines changed: 100 additions & 1 deletion

File tree

frontend/drivers/platform_unix.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2480,7 +2480,9 @@ static int frontend_unix_parse_drive_list(void *data, bool load_content)
24802480
(*env)->ExceptionClear(env);
24812481
}
24822482
else
2483-
for (jsize i = 0; i < trees_length; ++i)
2483+
{
2484+
jsize i;
2485+
for (i = 0; i < trees_length; ++i)
24842486
{
24852487
const char *tree_chars;
24862488
char *serialized_path;
@@ -2496,6 +2498,12 @@ static int frontend_unix_parse_drive_list(void *data, bool load_content)
24962498
{
24972499
(*env)->ExceptionDescribe(env);
24982500
(*env)->ExceptionClear(env);
2501+
(*env)->DeleteLocalRef(env, tree);
2502+
if ((*env)->ExceptionOccurred(env))
2503+
{
2504+
(*env)->ExceptionDescribe(env);
2505+
(*env)->ExceptionClear(env);
2506+
}
24992507
continue;
25002508
}
25012509
if ((serialized_path = retro_vfs_path_join_saf(tree_chars, "")) != NULL)
@@ -2513,7 +2521,20 @@ static int frontend_unix_parse_drive_list(void *data, bool load_content)
25132521
(*env)->ExceptionDescribe(env);
25142522
(*env)->ExceptionClear(env);
25152523
}
2524+
(*env)->DeleteLocalRef(env, tree);
2525+
if ((*env)->ExceptionOccurred(env))
2526+
{
2527+
(*env)->ExceptionDescribe(env);
2528+
(*env)->ExceptionClear(env);
2529+
}
25162530
}
2531+
}
2532+
(*env)->DeleteLocalRef(env, trees);
2533+
if ((*env)->ExceptionOccurred(env))
2534+
{
2535+
(*env)->ExceptionDescribe(env);
2536+
(*env)->ExceptionClear(env);
2537+
}
25172538
}
25182539
}
25192540

libretro-common/vfs/vfs_implementation_saf.c

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ bool retro_vfs_init_saf(JNIEnv *(*get_jni_env)(void), jobject activity_object)
5555
if (env == NULL)
5656
return false;
5757

58+
(*env)->PushLocalFrame(env, 14);
59+
if ((*env)->ExceptionOccurred(env))
60+
{
61+
(*env)->ExceptionDescribe(env);
62+
(*env)->ExceptionClear(env);
63+
return false;
64+
}
65+
5866
vfs_saf_content_resolver_object = NULL;
5967
vfs_saf_vfs_implementation_saf_class = NULL;
6068
vfs_saf_saf_stat_class = NULL;
@@ -172,6 +180,7 @@ bool retro_vfs_init_saf(JNIEnv *(*get_jni_env)(void), jobject activity_object)
172180
}
173181

174182
vfs_saf_get_jni_env = get_jni_env;
183+
(*env)->PopLocalFrame(env, NULL);
175184
return true;
176185

177186
error:
@@ -201,6 +210,7 @@ bool retro_vfs_init_saf(JNIEnv *(*get_jni_env)(void), jobject activity_object)
201210
vfs_saf_saf_directory_class = NULL;
202211
if ((*env)->ExceptionOccurred(env)) goto error;
203212
}
213+
(*env)->PopLocalFrame(env, NULL);
204214
return false;
205215
}
206216

@@ -376,6 +386,14 @@ int retro_vfs_file_open_saf(const char *tree, const char *path, unsigned mode)
376386
if (env == NULL)
377387
return -1;
378388

389+
(*env)->PushLocalFrame(env, 2);
390+
if ((*env)->ExceptionOccurred(env))
391+
{
392+
(*env)->ExceptionDescribe(env);
393+
(*env)->ExceptionClear(env);
394+
return -1;
395+
}
396+
379397
tree_object = (*env)->NewStringUTF(env, tree);
380398
if ((*env)->ExceptionOccurred(env)) goto error;
381399

@@ -385,11 +403,13 @@ int retro_vfs_file_open_saf(const char *tree, const char *path, unsigned mode)
385403
fd = (*env)->CallStaticIntMethod(env, vfs_saf_vfs_implementation_saf_class, vfs_saf_open_saf_file_method, vfs_saf_content_resolver_object, tree_object, path_object, !!(mode & RETRO_VFS_FILE_ACCESS_READ), !!(mode & RETRO_VFS_FILE_ACCESS_WRITE), !(mode & RETRO_VFS_FILE_ACCESS_UPDATE_EXISTING));
386404
if ((*env)->ExceptionOccurred(env)) goto error;
387405

406+
(*env)->PopLocalFrame(env, NULL);
388407
return fd;
389408

390409
error:
391410
(*env)->ExceptionDescribe(env);
392411
(*env)->ExceptionClear(env);
412+
(*env)->PopLocalFrame(env, NULL);
393413
return -1;
394414
}
395415

@@ -406,6 +426,14 @@ int retro_vfs_file_remove_saf(const char *tree, const char *path)
406426
if (env == NULL)
407427
return -1;
408428

429+
(*env)->PushLocalFrame(env, 2);
430+
if ((*env)->ExceptionOccurred(env))
431+
{
432+
(*env)->ExceptionDescribe(env);
433+
(*env)->ExceptionClear(env);
434+
return -1;
435+
}
436+
409437
tree_object = (*env)->NewStringUTF(env, tree);
410438
if ((*env)->ExceptionOccurred(env)) goto error;
411439

@@ -415,11 +443,13 @@ int retro_vfs_file_remove_saf(const char *tree, const char *path)
415443
ret = (*env)->CallStaticBooleanMethod(env, vfs_saf_vfs_implementation_saf_class, vfs_saf_remove_saf_file_method, vfs_saf_content_resolver_object, tree_object, path_object);
416444
if ((*env)->ExceptionOccurred(env)) goto error;
417445

446+
(*env)->PopLocalFrame(env, NULL);
418447
return ret ? 0 : -1;
419448

420449
error:
421450
(*env)->ExceptionDescribe(env);
422451
(*env)->ExceptionClear(env);
452+
(*env)->PopLocalFrame(env, NULL);
423453
return -1;
424454
}
425455

@@ -443,6 +473,14 @@ int retro_vfs_stat_saf(const char *tree, const char *path, int32_t *size)
443473
if (env == NULL)
444474
return 0;
445475

476+
(*env)->PushLocalFrame(env, 3);
477+
if ((*env)->ExceptionOccurred(env))
478+
{
479+
(*env)->ExceptionDescribe(env);
480+
(*env)->ExceptionClear(env);
481+
return 0;
482+
}
483+
446484
tree_object = (*env)->NewStringUTF(env, tree);
447485
if ((*env)->ExceptionOccurred(env)) goto error;
448486

@@ -457,14 +495,20 @@ int retro_vfs_stat_saf(const char *tree, const char *path, int32_t *size)
457495
saf_stat_size = (*env)->CallLongMethod(env, saf_stat, vfs_saf_saf_stat_get_size_method);
458496
if ((*env)->ExceptionOccurred(env)) goto error;
459497
if (saf_stat_size < 0)
498+
{
499+
(*env)->PopLocalFrame(env, NULL);
460500
return 0;
501+
}
461502
}
462503
else
463504
{
464505
bool saf_stat_is_open = (*env)->CallBooleanMethod(env, saf_stat, vfs_saf_saf_stat_get_is_open_method);
465506
if ((*env)->ExceptionOccurred(env)) goto error;
466507
if (!saf_stat_is_open)
508+
{
509+
(*env)->PopLocalFrame(env, NULL);
467510
return 0;
511+
}
468512
}
469513

470514
saf_stat_is_directory = (*env)->CallBooleanMethod(env, saf_stat, vfs_saf_saf_stat_get_is_directory_method);
@@ -473,11 +517,13 @@ int retro_vfs_stat_saf(const char *tree, const char *path, int32_t *size)
473517
if (size != NULL)
474518
*size = saf_stat_size > INT32_MAX ? INT32_MAX : (int32_t)saf_stat_size;
475519

520+
(*env)->PopLocalFrame(env, NULL);
476521
return saf_stat_is_directory ? RETRO_VFS_STAT_IS_VALID | RETRO_VFS_STAT_IS_DIRECTORY : RETRO_VFS_STAT_IS_VALID;
477522

478523
error:
479524
(*env)->ExceptionDescribe(env);
480525
(*env)->ExceptionClear(env);
526+
(*env)->PopLocalFrame(env, NULL);
481527
return 0;
482528
}
483529

@@ -494,6 +540,14 @@ int retro_vfs_mkdir_saf(const char *tree, const char *dir)
494540
if (env == NULL)
495541
return -1;
496542

543+
(*env)->PushLocalFrame(env, 2);
544+
if ((*env)->ExceptionOccurred(env))
545+
{
546+
(*env)->ExceptionDescribe(env);
547+
(*env)->ExceptionClear(env);
548+
return -1;
549+
}
550+
497551
tree_object = (*env)->NewStringUTF(env, tree);
498552
if ((*env)->ExceptionOccurred(env)) goto error;
499553

@@ -503,11 +557,13 @@ int retro_vfs_mkdir_saf(const char *tree, const char *dir)
503557
ret = (*env)->CallStaticIntMethod(env, vfs_saf_vfs_implementation_saf_class, vfs_saf_mkdir_saf_method, vfs_saf_content_resolver_object, tree_object, path_object);
504558
if ((*env)->ExceptionOccurred(env)) goto error;
505559

560+
(*env)->PopLocalFrame(env, NULL);
506561
return ret;
507562

508563
error:
509564
(*env)->ExceptionDescribe(env);
510565
(*env)->ExceptionClear(env);
566+
(*env)->PopLocalFrame(env, NULL);
511567
return -1;
512568
}
513569

@@ -528,6 +584,15 @@ libretro_vfs_implementation_saf_dir *retro_vfs_opendir_saf(const char *tree, con
528584
if (dirstream == NULL)
529585
return NULL;
530586

587+
(*env)->PushLocalFrame(env, 2);
588+
if ((*env)->ExceptionOccurred(env))
589+
{
590+
free(dirstream);
591+
(*env)->ExceptionDescribe(env);
592+
(*env)->ExceptionClear(env);
593+
return NULL;
594+
}
595+
531596
tree_object = (*env)->NewStringUTF(env, tree);
532597
if ((*env)->ExceptionOccurred(env)) goto error;
533598

@@ -543,12 +608,14 @@ libretro_vfs_implementation_saf_dir *retro_vfs_opendir_saf(const char *tree, con
543608
dirstream->dirent_name_object = NULL;
544609
dirstream->dirent_name = NULL;
545610
dirstream->dirent_is_dir = false;
611+
(*env)->PopLocalFrame(env, NULL);
546612
return dirstream;
547613

548614
error:
549615
free(dirstream);
550616
(*env)->ExceptionDescribe(env);
551617
(*env)->ExceptionClear(env);
618+
(*env)->PopLocalFrame(env, NULL);
552619
return NULL;
553620
}
554621

@@ -565,6 +632,14 @@ bool retro_vfs_readdir_saf(libretro_vfs_implementation_saf_dir *dirstream)
565632
if (env == NULL)
566633
return false;
567634

635+
(*env)->PushLocalFrame(env, 1);
636+
if ((*env)->ExceptionOccurred(env))
637+
{
638+
(*env)->ExceptionDescribe(env);
639+
(*env)->ExceptionClear(env);
640+
return false;
641+
}
642+
568643
ret = (*env)->CallBooleanMethod(env, dirstream->directory_object, vfs_saf_saf_directory_readdir_method);
569644
if ((*env)->ExceptionOccurred(env)) goto error;
570645

@@ -584,6 +659,7 @@ bool retro_vfs_readdir_saf(libretro_vfs_implementation_saf_dir *dirstream)
584659
if (!ret)
585660
{
586661
dirstream->dirent_is_dir = false;
662+
(*env)->PopLocalFrame(env, NULL);
587663
return false;
588664
}
589665

@@ -601,6 +677,7 @@ bool retro_vfs_readdir_saf(libretro_vfs_implementation_saf_dir *dirstream)
601677
if ((*env)->ExceptionOccurred(env)) goto error;
602678
dirstream->dirent_name = dirent_name;
603679

680+
(*env)->PopLocalFrame(env, NULL);
604681
return true;
605682

606683
error:
@@ -619,6 +696,7 @@ bool retro_vfs_readdir_saf(libretro_vfs_implementation_saf_dir *dirstream)
619696
if ((*env)->ExceptionOccurred(env)) goto error;
620697
}
621698
dirstream->dirent_is_dir = false;
699+
(*env)->PopLocalFrame(env, NULL);
622700
return false;
623701
}
624702

0 commit comments

Comments
 (0)