Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 60ff9761 authored by Jerome Gaillard's avatar Jerome Gaillard
Browse files

Throw Java exceptions when failing to load ICU data

When attempting to load the ICU data upon initializing JNI for
libandroid_runtime, this throws Java exceptions if there is an issue.
This can prevent later crashes if the data did not load correctly.

Flag: NONE host-only change
Bug: 396462924
Test: N/A
Change-Id: Ib50b24f7d12343669e0d32a676ee09072cb7c76c
parent bdf3b189
Loading
Loading
Loading
Loading
+10 −4
Original line number Diff line number Diff line
@@ -280,12 +280,18 @@ static string getJavaProperty(JNIEnv* env, const char* property_name,
    return string(chars.c_str());
}

static void loadIcuData(string icuPath) {
static void loadIcuData(JNIEnv* env, string icuPath) {
    void* addr = mmapFile(icuPath.c_str());
    if (addr == nullptr) {
        jniThrowRuntimeException(env, "Failed to map the ICU data file.");
    }
    UErrorCode err = U_ZERO_ERROR;
    udata_setCommonData(addr, &err);
    if (err != U_ZERO_ERROR) {
        ALOGE("Unable to load ICU data\n");
        jniThrowRuntimeException(env,
                                 format("udata_setCommonData failed with error code {}",
                                        u_errorName(err))
                                         .c_str());
    }
}

@@ -296,12 +302,12 @@ static void loadIcuData() {
    JNIEnv* env = AndroidRuntime::getJNIEnv();
    string icuPath = base::GetProperty("ro.icu.data.path", "");
    if (!icuPath.empty()) {
        loadIcuData(icuPath);
        loadIcuData(env, icuPath);
    } else {
        // fallback to read from java.lang.System.getProperty
        string icuPathFromJava = getJavaProperty(env, "icu.data.path");
        if (!icuPathFromJava.empty()) {
            loadIcuData(icuPathFromJava);
            loadIcuData(env, icuPathFromJava);
        }
    }