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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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));
 
         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%%", (uint32_t)(((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,
+                (uint32_t)(((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) : "");
 
-- 
2.11.0



[Index of Archives]     [Linux Audio Users]     [AMD Graphics]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux