Repository: dotnet/runtime
File: src/tasks/AppleAppBuilder/Templates/runtime-coreclr.m (Objective-C)

Code before:
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#import <Foundation/Foundation.h>
#include "coreclrhost.h"
#include "host_runtime_contract.h"
#include <TargetConditionals.h>
#import <os/log.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>

#import "util.h"

#define APPLE_RUNTIME_IDENTIFIER "//%APPLE_RUNTIME_IDENTIFIER%"

#define MAX_LOADED_NATIVE_CODE_COUNT 64 // Arbitrarily 'large enough' number
static void* loaded_native_code_handles[MAX_LOADED_NATIVE_CODE_COUNT];
static int loaded_native_code_handle_count = 0;

const char *
get_bundle_path (void)
{
    static char *bundle_path = NULL;
    if (bundle_path)
        return bundle_path;
    NSBundle* main_bundle = [NSBundle mainBundle];
    NSString* path = [main_bundle bundlePath];

#if TARGET_OS_MACCATALYST
    path = [path stringByAppendingString:@"/Contents/Resources"];
#endif

    bundle_path = strdup ([path UTF8String]);

    return bundle_path;
}

char *
compute_trusted_platform_assemblies ()
{
    const char *bundle_path = get_bundle_path ();

    NSMutableArray<NSString *> *files = [NSMutableArray array];
    NSMutableArray<NSString *> *exes = [NSMutableArray array];

Review comment (bug):
The comment describes the limit as "Arbitrarily 'large enough'" but this is a hard limit that could be exceeded in practice. Consider documenting the rationale for choosing 64, or explain what happens when this limit is exceeded (currently it would cause undefined behavior due to the assert on line 135).
```suggestion
// Maximum number of native code handles that can be loaded.
// This is a hard limit; exceeding it will result in an error and the handle will not be added.
// If you need to load more than 64 native code handles, increase this value or refactor to use dynamic allocation.
#define MAX_LOADED_NATIVE_CODE_COUNT 64
static void* loaded_native_code_handles[MAX_LOADED_NATIVE_CODE_COUNT];
static int loaded_native_code_handle_count = 0;

// Adds a native code handle to the loaded_native_code_handles array.
// Returns 0 on success, -1 if the limit is exceeded.
static int add_loaded_native_code_handle(void* handle)
{
    if (loaded_native_code_handle_count >= MAX_LOADED_NATIVE_CODE_COUNT) {
        fprintf(stderr, "Error: Exceeded maximum number of loaded native code handles (%d)\n", MAX_LOADED_NATIVE_CODE_COUNT);
        return -1;
    }
    loaded_native_code_handles[loaded_native_code_handle_count++] = handle;
    return 0;
}
```

Code after:
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#import <Foundation/Foundation.h>
#include "coreclrhost.h"
#include "host_runtime_contract.h"
#include <TargetConditionals.h>
#import <os/log.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>

#import "util.h"

#define APPLE_RUNTIME_IDENTIFIER "//%APPLE_RUNTIME_IDENTIFIER%"

const char *
get_bundle_path (void)
{
    static char *bundle_path = NULL;
    if (bundle_path)
        return bundle_path;
    NSBundle* main_bundle = [NSBundle mainBundle];
    NSString* path = [main_bundle bundlePath];

#if TARGET_OS_MACCATALYST
    path = [path stringByAppendingString:@"/Contents/Resources"];
#endif

    bundle_path = strdup ([path UTF8String]);

    return bundle_path;
}

char *
compute_trusted_platform_assemblies ()
{
    const char *bundle_path = get_bundle_path ();

    NSMutableArray<NSString *> *files = [NSMutableArray array];
    NSMutableArray<NSString *> *exes = [NSMutableArray array];

    NSFileManager *manager = [NSFileManager defaultManager];
    NSString *dir = [NSString stringWithUTF8String: bundle_path];
    NSDirectoryEnumerator *enumerator = [manager enumeratorAtURL:[NSURL fileURLWithPath: dir]