The three variables x, s, n are all of unsigned type, and the
subtraction x - s / n may underflow, resulting in a very large positive
value. This should not matter since we square the difference, but on
the other hand, the underflow can easily be avoided. This patch uses
a temporary variable to do so.
static int add_afs_statistics(const struct osl_row *row)
{
- uint64_t n, x, s;
+ uint64_t n, x, s, q;
struct afs_info afsi;
int ret;
n = statistics.num;
x = afsi.last_played;
s = statistics.last_played_sum;
- if (n > 0)
- statistics.last_played_qd += (x - s / n) * (x - s / n) * n / (n + 1);
+ if (n > 0) {
+ q = (x > s / n)? x - s / n : s / n - x;
+ statistics.last_played_qd += q * q * n / (n + 1);
+ }
statistics.last_played_sum += x;
x = afsi.num_played;
s = statistics.num_played_sum;
- if (n > 0)
- statistics.num_played_qd += (x - s / n) * (x - s / n) * n / (n + 1);
+ if (n > 0) {
+ q = (x > s / n)? x - s / n : s / n - x;
+ statistics.num_played_qd += q * q * n / (n + 1);
+ }
statistics.num_played_sum += x;
statistics.num++;
return 1;