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

Code before:
    setenv ("DOTNET_SYSTEM_GLOBALIZATION_HYBRID", "1", TRUE);
#endif

    // build using DiagnosticPorts property in AppleAppBuilder
    // or set DOTNET_DiagnosticPorts env via mlaunch, xharness when undefined.
    // NOTE, using DOTNET_DiagnosticPorts requires app build using AppleAppBuilder and RuntimeComponents=diagnostics_tracing
#ifdef DIAGNOSTIC_PORTS
    setenv ("DOTNET_DiagnosticPorts", DIAGNOSTIC_PORTS, true);
#endif

%EnvVariables%

    char **managed_argv;
    int argi = get_managed_args (&managed_argv);

    bool wait_for_debugger = FALSE;

    const char* bundle = get_bundle_path ();
    chdir (bundle);

    char icu_dat_path [1024];
    int res;
#if defined(HYBRID_GLOBALIZATION)
    res = snprintf (icu_dat_path, sizeof (icu_dat_path) - 1, "%s/%s", bundle, "icudt_hybrid.dat");
#else
    res = snprintf (icu_dat_path, sizeof (icu_dat_path) - 1, "%s/%s", bundle, "icudt.dat");
#endif
    assert (res > 0);

    struct host_runtime_contract host_contract = {
        .size = sizeof(struct host_runtime_contract),
        .pinvoke_override = &pinvoke_override,
        .get_native_code_data = &get_native_code_data
    };

    char contract_str[19]; // 0x + 16 hex digits + '\0'
    snprintf(contract_str, 19, "0x%zx", (size_t)(&host_contract));

    // TODO: set TRUSTED_PLATFORM_ASSEMBLIES, APP_PATHS and NATIVE_DLL_SEARCH_DIRECTORIES
    const char *appctx_keys [] = {
        "RUNTIME_IDENTIFIER",
        "APP_CONTEXT_BASE_DIRECTORY",
        "TRUSTED_PLATFORM_ASSEMBLIES",
        "HOST_RUNTIME_CONTRACT",
#if !defined(INVARIANT_GLOBALIZATION)
        "ICU_DAT_FILE_PATH"
#endif
    };
    const char *appctx_values [] = {
        APPLE_RUNTIME_IDENTIFIER,
        bundle,

Review comment (suggestion):
The buffer size calculation `char contract_str[19];` is based on specific assumptions (0x prefix + 16 hex digits for a pointer + null terminator). However, on some platforms, pointers may be larger or smaller than 64 bits. Consider using a more portable approach like `char contract_str[32];` or calculating the size based on `sizeof(void*)`. The comment should also explain the size calculation.
```suggestion
    // Buffer size: "0x" + 2 hex digits per byte of pointer + null terminator.
    char contract_str[2 + sizeof(void*) * 2 + 1];
    snprintf(contract_str, sizeof(contract_str), "0x%zx", (size_t)(&host_contract));
```

Code after:
    char **managed_argv;
    int argi = get_managed_args (&managed_argv);

    bool wait_for_debugger = FALSE;

    const char* bundle = get_bundle_path ();
    chdir (bundle);

    char icu_dat_path [1024];
    int res;
#if defined(HYBRID_GLOBALIZATION)
    res = snprintf (icu_dat_path, sizeof (icu_dat_path) - 1, "%s/%s", bundle, "icudt_hybrid.dat");
#else
    res = snprintf (icu_dat_path, sizeof (icu_dat_path) - 1, "%s/%s", bundle, "icudt.dat");
#endif
    assert (res > 0);

    // Contract lasts the lifetime of the app. The app exists before the end of this function.
    struct host_runtime_contract host_contract = {
        .size = sizeof(struct host_runtime_contract),
        .pinvoke_override = &pinvoke_override,
        .get_native_code_data = &get_native_code_data
    };

    char contract_str[19]; // 0x + 16 hex digits + '\0'
    snprintf(contract_str, 19, "0x%zx", (size_t)(&host_contract));

    // TODO: set TRUSTED_PLATFORM_ASSEMBLIES, APP_PATHS and NATIVE_DLL_SEARCH_DIRECTORIES
    const char *appctx_keys [] = {
        "RUNTIME_IDENTIFIER",
        "APP_CONTEXT_BASE_DIRECTORY",
        "TRUSTED_PLATFORM_ASSEMBLIES",
        "HOST_RUNTIME_CONTRACT",
#if !defined(INVARIANT_GLOBALIZATION)
        "ICU_DAT_FILE_PATH"
#endif
    };
    const char *appctx_values [] = {
        APPLE_RUNTIME_IDENTIFIER,
        bundle,
        compute_trusted_platform_assemblies(),
        contract_str,
#if !defined(INVARIANT_GLOBALIZATION)
        icu_dat_path
#endif
    };

    const char* executable = "%EntryPointLibName%";
    const char *executablePath = [[[[NSBundle mainBundle] executableURL] path] UTF8String];
    unsigned int coreclr_domainId = 0;
    void *coreclr_handle = NULL;