mood.c: Avoid integer overflow.
authorAndre Noll <maan@tuebingen.mpg.de>
Tue, 15 Mar 2016 19:41:37 +0000 (20:41 +0100)
committerAndre Noll <maan@tuebingen.mpg.de>
Tue, 15 Mar 2016 20:59:29 +0000 (21:59 +0100)
The quadratic deviation qd can be very large, causing the
multiplication n * qd to overflow. The new code avoids the
multiplication at the cost of two calls to int_sqrt() instead of one.

mood.c

diff --git a/mood.c b/mood.c
index 8e6a766..e026c96 100644 (file)
--- a/mood.c
+++ b/mood.c
@@ -472,7 +472,7 @@ static int64_t normalized_value(int64_t x, int64_t n, int64_t sum, int64_t qd)
 {
        if (!n || !qd)
                return 0;
-       return 100 * (n * x - sum) / (int64_t)int_sqrt(n * qd);
+       return 100 * (n * x - sum) / (int64_t)int_sqrt(n) / (int64_t)int_sqrt(qd);
 }
 
 static long compute_num_played_score(struct afs_info *afsi)