From 37e58f8cfacde4163735cf1be8b89f6ec06144c2 Mon Sep 17 00:00:00 2001
From: VaL Doroshchuk <valentyn.doroshchuk@qt.io>
Date: Wed, 20 Sep 2017 18:01:56 +0200
Subject: [PATCH] PulseAudio: Reject unsupported audio formats

Based on https://freedesktop.org/software/pulseaudio/doxygen/sample.html

Applied a fix to allow only supported combinations sample type/sample size/byte order.
If the combination has not been found PA_SAMPLE_INVALID is returned.

Task-number: QTBUG-62621
Change-Id: I14c3d3828a0527aef0a5afa753fb640ead0cc18d
Reviewed-by: Christian Stromme <christian.stromme@qt.io>
---
 src/plugins/pulseaudio/qpulsehelpers.cpp | 39 ++++++++++--------------
 1 file changed, 16 insertions(+), 23 deletions(-)

diff --git a/src/plugins/pulseaudio/qpulsehelpers.cpp b/src/plugins/pulseaudio/qpulsehelpers.cpp
index 17579bdd9..0604c97f5 100644
--- a/src/plugins/pulseaudio/qpulsehelpers.cpp
+++ b/src/plugins/pulseaudio/qpulsehelpers.cpp
@@ -49,30 +49,23 @@ pa_sample_spec audioFormatToSampleSpec(const QAudioFormat &format)
 
     spec.rate = format.sampleRate();
     spec.channels = format.channelCount();
-
-    if (format.sampleSize() == 8) {
-        spec.format = PA_SAMPLE_U8;
-    } else if (format.sampleSize() == 16) {
-        switch (format.byteOrder()) {
-        case QAudioFormat::BigEndian: spec.format = PA_SAMPLE_S16BE; break;
-        case QAudioFormat::LittleEndian: spec.format = PA_SAMPLE_S16LE; break;
-        }
-    } else if (format.sampleSize() == 24) {
-        switch (format.byteOrder()) {
-        case QAudioFormat::BigEndian: spec.format = PA_SAMPLE_S24BE; break;
-        case QAudioFormat::LittleEndian: spec.format = PA_SAMPLE_S24LE; break;
-        }
-    } else if (format.sampleSize() == 32) {
-        switch (format.byteOrder()) {
-        case QAudioFormat::BigEndian:
-            format.sampleType() == QAudioFormat::Float ? spec.format = PA_SAMPLE_FLOAT32BE : spec.format = PA_SAMPLE_S32BE;
-            break;
-        case QAudioFormat::LittleEndian:
-            format.sampleType() == QAudioFormat::Float ? spec.format = PA_SAMPLE_FLOAT32LE : spec.format = PA_SAMPLE_S32LE;
-            break;
+    spec.format = PA_SAMPLE_INVALID;
+    const bool isBigEndian = (format.byteOrder() == QAudioFormat::BigEndian);
+
+    if (format.sampleType() == QAudioFormat::UnSignedInt) {
+        if (format.sampleSize() == 8)
+            spec.format = PA_SAMPLE_U8;
+    } else if (format.sampleType() == QAudioFormat::SignedInt) {
+        if (format.sampleSize() == 16) {
+            spec.format = isBigEndian ? PA_SAMPLE_S16BE : PA_SAMPLE_S16LE;
+        } else if (format.sampleSize() == 24) {
+            spec.format = isBigEndian ? PA_SAMPLE_S24BE : PA_SAMPLE_S24LE;
+        } else if (format.sampleSize() == 32) {
+            spec.format = isBigEndian ? PA_SAMPLE_S32BE : PA_SAMPLE_S32LE;
         }
-    } else {
-        spec.format = PA_SAMPLE_INVALID;
+    } else if (format.sampleType() == QAudioFormat::Float) {
+        if (format.sampleSize() == 32)
+            spec.format = isBigEndian ? PA_SAMPLE_FLOAT32BE : PA_SAMPLE_FLOAT32LE;
     }
 
     return spec;
-- 
GitLab