[pulseaudio-discuss] [PATCH 3/3] volume: Fix overflow in percent calculation of pa_*volume_snprint*()

Tanu Kaskinen tanuk at iki.fi
Sat Apr 29 08:27:44 UTC 2017


On Fri, 2017-04-28 at 11:13 +0200, Georg Chini wrote:
> The percent calculation could overflow in the pa_*volume_snprint*() functions.
> For large volumes, volume * 100 can exceed UINT32_MAX.
> 
> This patch adds appropriate type casts.
> ---
>  src/pulse/volume.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/src/pulse/volume.c b/src/pulse/volume.c
> index 73e78370..a135e57d 100644
> --- a/src/pulse/volume.c
> +++ b/src/pulse/volume.c
> @@ -313,7 +313,7 @@ char *pa_cvolume_snprint(char *s, size_t l, const pa_cvolume *c) {
>          l -= pa_snprintf(e, l, "%s%u: %3u%%",
>                        first ? "" : " ",
>                        channel,
> -                      (c->values[channel]*100+PA_VOLUME_NORM/2)/PA_VOLUME_NORM);
> +                      (uint32_t)(((uint64_t)c->values[channel] * 100 + (uint64_t)PA_VOLUME_NORM / 2) / (uint64_t)PA_VOLUME_NORM));

"%u" is the symbol for unsigned, so you should cast to unsigned rather
than uint32_t, or change the format string to use "%" PRIu32 instead of
"%u" (I'd prefer %u, because using PRIu32 looks ugly, even though it
would be slightly more correct in this case, since pa_volume_t is
always a 32-bit number).

-- 
Tanu

https://www.patreon.com/tanuk


More information about the pulseaudio-discuss mailing list