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

Commit b7a10b7c authored by Ytai Ben-Tsvi's avatar Ytai Ben-Tsvi
Browse files

Wrong arguments for JNI call

Two bugs are addressed here:
1. The original code passed 3 arguments to a method expecting two.
2. The UUID Java class expects the UUID bytes to be encoded as big-
   endian into the longs.

Test: Manual verification
Change-Id: Id597f31749ed8ff5be4b910c044f17df1be4da78
parent 2fccc3a6
Loading
Loading
Loading
Loading
+14 −1
Original line number Diff line number Diff line
@@ -133,6 +133,18 @@ nativeClassInit (JNIEnv *_env, jclass _this)
            MakeGlobalRefOrDie(_env, _env->CallObjectMethod(empty.get(), stringOffsets.intern));
}

uint64_t htonll(uint64_t ll) {
    constexpr uint32_t kBytesToTest = 0x12345678;
    constexpr uint8_t kFirstByte = (const uint8_t &)kBytesToTest;
    constexpr bool kIsLittleEndian = kFirstByte == 0x78;

    if constexpr (kIsLittleEndian) {
        return static_cast<uint64_t>(htonl(ll & 0xffffffff)) << 32 | htonl(ll >> 32);
    } else {
        return ll;
    }
}

static jstring getJavaInternedString(JNIEnv *env, const String8 &string) {
    if (string == "") {
        return gStringOffsets.emptyString;
@@ -193,7 +205,8 @@ translateNativeSensorToJavaSensor(JNIEnv *env, jobject sensor, const Sensor& nat
        int32_t id = nativeSensor.getId();
        env->CallVoidMethod(sensor, sensorOffsets.setId, id);
        Sensor::uuid_t uuid = nativeSensor.getUuid();
        env->CallVoidMethod(sensor, sensorOffsets.setUuid, id, uuid.i64[0], uuid.i64[1]);
        env->CallVoidMethod(sensor, sensorOffsets.setUuid, htonll(uuid.i64[0]),
                            htonll(uuid.i64[1]));
    }
    return sensor;
}