From a7bda88e6b628c4c9879cac2b4b4c68da3a1a6b4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jean-Micha=C3=ABl=20Celerier?= Date: Mon, 21 Dec 2020 19:22:16 +0100 Subject: [PATCH] Fix a GCC warning due to unsigned substraction 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 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hostapi/alsa/pa_linux_alsa.c b/src/hostapi/alsa/pa_linux_alsa.c index 462034c..be76c64 100644 --- a/src/hostapi/alsa/pa_linux_alsa.c +++ b/src/hostapi/alsa/pa_linux_alsa.c @@ -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; -- 2.43.0