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

Commit 2b971b34 authored by Andy Hung's avatar Andy Hung Committed by android-build-merger
Browse files

AudioSystem: Verify audio port array information am: 737d9fb1

am: 79d51bf4

Change-Id: Ie74dc2cc9e8f8f9fca143f7cb83acec2b8af669b
parents 001e7226 79d51bf4
Loading
Loading
Loading
Loading
+41 −2
Original line number Original line Diff line number Diff line
@@ -20,13 +20,14 @@
#define LOG_TAG "AudioSystem-JNI"
#define LOG_TAG "AudioSystem-JNI"
#include <utils/Log.h>
#include <utils/Log.h>


#include <sstream>
#include <jni.h>
#include <jni.h>
#include <JNIHelp.h>
#include <JNIHelp.h>
#include "core_jni_helpers.h"
#include "core_jni_helpers.h"


#include <media/AudioSystem.h>
#include <media/AudioSystem.h>
#include <media/AudioPolicy.h>
#include <media/AudioPolicy.h>

#include <nativehelper/ScopedLocalRef.h>
#include <system/audio.h>
#include <system/audio.h>
#include <system/audio_policy.h>
#include <system/audio_policy.h>
#include "android_media_AudioFormat.h"
#include "android_media_AudioFormat.h"
@@ -903,6 +904,12 @@ static bool hasFormat(int* formats, size_t size, int format) {
    return false; // not found
    return false; // not found
}
}


// TODO: pull out to separate file
template <typename T, size_t N>
static constexpr size_t array_size(const T (&)[N]) {
    return N;
}

static jint convertAudioPortFromNative(JNIEnv *env,
static jint convertAudioPortFromNative(JNIEnv *env,
                                           jobject *jAudioPort, const struct audio_port *nAudioPort)
                                           jobject *jAudioPort, const struct audio_port *nAudioPort)
{
{
@@ -923,6 +930,38 @@ static jint convertAudioPortFromNative(JNIEnv *env,
    ALOGV("convertAudioPortFromNative id %d role %d type %d name %s",
    ALOGV("convertAudioPortFromNative id %d role %d type %d name %s",
        nAudioPort->id, nAudioPort->role, nAudioPort->type, nAudioPort->name);
        nAudioPort->id, nAudioPort->role, nAudioPort->type, nAudioPort->name);


    // Verify audio port array count info.
    if (nAudioPort->num_sample_rates > array_size(nAudioPort->sample_rates)
            || nAudioPort->num_channel_masks > array_size(nAudioPort->channel_masks)
            || nAudioPort->num_formats > array_size(nAudioPort->formats)
            || nAudioPort->num_gains > array_size(nAudioPort->gains)) {

        std::stringstream ss;
        ss << "convertAudioPortFromNative array count out of bounds:"
                << " num_sample_rates " << nAudioPort->num_sample_rates
                << " num_channel_masks " << nAudioPort->num_channel_masks
                << " num_formats " << nAudioPort->num_formats
                << " num_gains " << nAudioPort->num_gains
                ;
        std::string s = ss.str();

        // Prefer to log through Java wtf instead of native ALOGE.
        ScopedLocalRef<jclass> jLogClass(env, env->FindClass("android/util/Log"));
        jmethodID jWtfId = (jLogClass.get() == nullptr)
                ? nullptr
                : env->GetStaticMethodID(jLogClass.get(), "wtf",
                        "(Ljava/lang/String;Ljava/lang/String;)I");
        if (jWtfId != nullptr) {
            ScopedLocalRef<jstring> jMessage(env, env->NewStringUTF(s.c_str()));
            ScopedLocalRef<jstring> jTag(env, env->NewStringUTF(LOG_TAG));
            (void)env->CallStaticIntMethod(jLogClass.get(), jWtfId, jTag.get(), jMessage.get());
        } else {
            ALOGE("%s", s.c_str());
        }
        jStatus = (jint)AUDIO_JAVA_ERROR;
        goto exit;
    }

    jSamplingRates = env->NewIntArray(nAudioPort->num_sample_rates);
    jSamplingRates = env->NewIntArray(nAudioPort->num_sample_rates);
    if (jSamplingRates == NULL) {
    if (jSamplingRates == NULL) {
        jStatus = (jint)AUDIO_JAVA_ERROR;
        jStatus = (jint)AUDIO_JAVA_ERROR;
@@ -1066,7 +1105,7 @@ static jint convertAudioPortFromNative(JNIEnv *env,
                                                       &jAudioPortConfig,
                                                       &jAudioPortConfig,
                                                       &nAudioPort->active_config);
                                                       &nAudioPort->active_config);
    if (jStatus != AUDIO_JAVA_SUCCESS) {
    if (jStatus != AUDIO_JAVA_SUCCESS) {
        return jStatus;
        goto exit;
    }
    }


    env->SetObjectField(*jAudioPort, gAudioPortFields.mActiveConfig, jAudioPortConfig);
    env->SetObjectField(*jAudioPort, gAudioPortFields.mActiveConfig, jAudioPortConfig);