]> Repos - portaudio/commitdiff
Fix a GCC warning due to unsigned substraction
authorJean-Michaël Celerier <jeanmichael.celerier@gmail.com>
Mon, 21 Dec 2020 18:22:16 +0000 (19:22 +0100)
committerPhil Burk <philburk@mobileer.com>
Sat, 26 Dec 2020 23:41:53 +0000 (15:41 -0800)
Given

    unsigned int reqRate, setRate, deviation;

then

    setRate - reqRate

is also an unsigned int - the code works "by chance" as `abs` casts that to an int which overflows and is technically undefined behaviour, for instance for setRate = 44100 and reqRate = 48000.

Thus cast them to int when computing the absolute value to ensure no UB.

src/hostapi/alsa/pa_linux_alsa.c

index 462034c027545dbbdbd6a66a7d306574f43f5356..be76c6452b8946e315081b00570f9d732087a32c 100644 (file)
@@ -3204,7 +3204,7 @@ static int SetApproximateSampleRate( snd_pcm_t *pcm, snd_pcm_hw_params_t *hwPara
     ENSURE_( alsa_snd_pcm_hw_params_set_rate_near( pcm, hwParams, &setRate, NULL ), paUnanticipatedHostError );
     /* The value actually set will be put in 'setRate' (may be way off); check the deviation as a proportion
      * of the requested-rate with reference to the max-deviate-ratio (larger values allow less deviation) */
-    deviation = abs( setRate - reqRate );
+    deviation = abs( (int)setRate - (int)reqRate );
     if( deviation > 0 && deviation * RATE_MAX_DEVIATE_RATIO > reqRate )
         result = paInvalidSampleRate;