volume: Fix overflow in percent calculation of pa_*volume_snprint*()
authorGeorg Chini <georg@chini.tk>
Sat, 29 Apr 2017 08:38:05 +0000 (10:38 +0200)
committerGeorg Chini <georg@chini.tk>
Sat, 29 Apr 2017 08:38:05 +0000 (10:38 +0200)
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

index 1488028..ffd42ec 100644 (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%%",
                       first ? "" : " ",
                       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);
         first = false;
@@ -333,7 +333,7 @@ char *pa_volume_snprint(char *s, size_t l, pa_volume_t v) {
         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;
 }
 
@@ -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",
                 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 ? pa_sw_volume_snprint_dB(dB, sizeof(dB), v) : "");