volume: Fix overflow in percent calculation of pa_*volume_snprint*()

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.
This commit is contained in:
Georg Chini 2017-04-29 10:38:05 +02:00
parent be1276d816
commit 68203100ff

View file

@ -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%%", l -= pa_snprintf(e, l, "%s%u: %3u%%",
first ? "" : " ", first ? "" : " ",
channel, channel,
(c->values[channel]*100+PA_VOLUME_NORM/2)/PA_VOLUME_NORM); (unsigned)(((uint64_t)c->values[channel] * 100 + (uint64_t)PA_VOLUME_NORM / 2) / (uint64_t)PA_VOLUME_NORM));
e = strchr(e, 0); e = strchr(e, 0);
first = false; first = false;
@ -333,7 +333,7 @@ char *pa_volume_snprint(char *s, size_t l, pa_volume_t v) {
return s; return s;
} }
pa_snprintf(s, l, "%3u%%", (v*100+PA_VOLUME_NORM/2)/PA_VOLUME_NORM); pa_snprintf(s, l, "%3u%%", (unsigned)(((uint64_t)v * 100 + (uint64_t)PA_VOLUME_NORM / 2) / (uint64_t)PA_VOLUME_NORM));
return s; return s;
} }
@ -446,7 +446,7 @@ char *pa_volume_snprint_verbose(char *s, size_t l, pa_volume_t v, int print_dB)
pa_snprintf(s, l, "%" PRIu32 " / %3u%%%s%s", pa_snprintf(s, l, "%" PRIu32 " / %3u%%%s%s",
v, v,
(v * 100 + PA_VOLUME_NORM / 2) / PA_VOLUME_NORM, (unsigned)(((uint64_t)v * 100 + (uint64_t)PA_VOLUME_NORM / 2) / (uint64_t)PA_VOLUME_NORM),
print_dB ? " / " : "", print_dB ? " / " : "",
print_dB ? pa_sw_volume_snprint_dB(dB, sizeof(dB), v) : ""); print_dB ? pa_sw_volume_snprint_dB(dB, sizeof(dB), v) : "");