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

Commit 16a2cc2f authored by qctecmdr's avatar qctecmdr Committed by Gerrit - the friendly Code Review server
Browse files

Merge "hal: Prevent integer overflow"

parents 14a4e172 62ebc724
Loading
Loading
Loading
Loading
+22 −10
Original line number Diff line number Diff line
@@ -4076,9 +4076,13 @@ static uint64_t get_actual_pcm_frames_rendered(struct stream_out *out, struct ti
     * which then needs protection, this causes delay in TS query for pcm_offload usecase
     * hence only estimate.
     */
    int64_t signed_frames = out->written - kernel_buffer_size;
    uint64_t signed_frames = 0;
    if (out->written >= kernel_buffer_size)
        signed_frames = out->written - kernel_buffer_size;

    signed_frames = signed_frames / (audio_bytes_per_sample(out->format) * popcount(out->channel_mask)) - platform_latency;
    signed_frames = signed_frames / (audio_bytes_per_sample(out->format) * popcount(out->channel_mask));
    if (signed_frames >= platform_latency)
        signed_frames = signed_frames - platform_latency;

    if (signed_frames > 0) {
        actual_frames_rendered = signed_frames;
@@ -4090,7 +4094,7 @@ static uint64_t get_actual_pcm_frames_rendered(struct stream_out *out, struct ti
    pthread_mutex_unlock(&out->position_query_lock);

    ALOGVV("%s signed frames %lld out_written %lld kernel_buffer_size %d"
            "bytes/sample %zu channel count %d", __func__,(long long int)signed_frames,
            "bytes/sample %zu channel count %d", __func__, signed_frames,
             (long long int)out->written, (int)kernel_buffer_size,
             audio_bytes_per_sample(out->compr_config.codec->format),
             popcount(out->channel_mask));
@@ -5833,25 +5837,33 @@ static int out_get_presentation_position(const struct audio_stream_out *stream,
            unsigned int avail;
            if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) {
                size_t kernel_buffer_size = out->config.period_size * out->config.period_count;
                int64_t signed_frames = out->written - kernel_buffer_size + avail;

                uint64_t signed_frames = 0;

                if (avail > kernel_buffer_size)
                    avail = kernel_buffer_size;

                if (out->written >= (kernel_buffer_size - avail))
                    signed_frames = out->written - kernel_buffer_size + avail;

                // This adjustment accounts for buffering after app processor.
                // It is based on estimated DSP latency per use case, rather than exact.
                if (signed_frames >= (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL))
                    signed_frames -=
                        (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL);

                // Adjustment accounts for A2dp encoder latency with non offload usecases
                // Note: Encoder latency is returned in ms, while platform_render_latency in us.
                if (AUDIO_DEVICE_OUT_ALL_A2DP & out->devices) {
                    if (signed_frames >= (audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000))
                        signed_frames -=
                            (audio_extn_a2dp_get_encoder_latency() * out->sample_rate / 1000);
                }

                // It would be unusual for this value to be negative, but check just in case ...
                if (signed_frames >= 0) {
                *frames = signed_frames;
                ret = 0;
            }
            }
        } else if (out->card_status == CARD_STATUS_OFFLINE) {
            *frames = out->written;
            clock_gettime(CLOCK_MONOTONIC, timestamp);