[gnome-software/wip/hughsie/histogram-crash: 1/2] Fix crash when viewing application details



commit c80eb2e767e9e1f9240ccec2d82bbc3d06754f98
Author: Richard Hughes <richard hughsie com>
Date:   Fri Feb 21 13:42:55 2020 +0000

    Fix crash when viewing application details
    
    The histogram code was dividing by zero if there were no ratings being set.

 src/gs-review-histogram.c | 28 +++++++++++++++-------------
 1 file changed, 15 insertions(+), 13 deletions(-)
---
diff --git a/src/gs-review-histogram.c b/src/gs-review-histogram.c
index bbd297c8..10b9f302 100644
--- a/src/gs-review-histogram.c
+++ b/src/gs-review-histogram.c
@@ -40,33 +40,35 @@ gs_review_histogram_set_ratings (GsReviewHistogram *histogram,
                                 GArray *review_ratings)
 {
        GsReviewHistogramPrivate *priv;
-       gdouble max;
-       gint count[5] = { 0, 0, 0, 0, 0 };
-       guint i;
+       gdouble fraction[5] = { 0.f, 0.f, 0.f, 0.f, 0.f };
+       guint count[5] = { 0, 0, 0, 0, 0 };
+       guint max = 0;
 
        g_return_if_fail (GS_IS_REVIEW_HISTOGRAM (histogram));
        priv = gs_review_histogram_get_instance_private (histogram);
 
-       /* Scale to maximum value */
-       for (max = 0, i = 0; i < review_ratings->len; i++) {
-               gint c;
-
-               c = g_array_index (review_ratings, gint, i);
+       /* scale to maximum value */
+       for (guint i = 0; i < review_ratings->len; i++) {
+               guint c = g_array_index (review_ratings, gint, i);
                if (c > max)
                        max = c;
                if (i > 0 && i < 6)
                        count[i - 1] = c;
        }
+       if (max > 0) {
+               for (guint i = 0; i < 5; i++)
+                       fraction[i] = (gdouble) count[i] / max;
+       }
 
-       gs_review_bar_set_fraction (GS_REVIEW_BAR (priv->bar5), count[4] / max);
+       gs_review_bar_set_fraction (GS_REVIEW_BAR (priv->bar5), fraction[4]);
        set_label (priv->label_count5, count[4]);
-       gs_review_bar_set_fraction (GS_REVIEW_BAR (priv->bar4), count[3] / max);
+       gs_review_bar_set_fraction (GS_REVIEW_BAR (priv->bar4), fraction[3]);
        set_label (priv->label_count4, count[3]);
-       gs_review_bar_set_fraction (GS_REVIEW_BAR (priv->bar3), count[2] / max);
+       gs_review_bar_set_fraction (GS_REVIEW_BAR (priv->bar3), fraction[2]);
        set_label (priv->label_count3, count[2]);
-       gs_review_bar_set_fraction (GS_REVIEW_BAR (priv->bar2), count[1] / max);
+       gs_review_bar_set_fraction (GS_REVIEW_BAR (priv->bar2), fraction[1]);
        set_label (priv->label_count2, count[1]);
-       gs_review_bar_set_fraction (GS_REVIEW_BAR (priv->bar1), count[0] / max);
+       gs_review_bar_set_fraction (GS_REVIEW_BAR (priv->bar1), fraction[0]);
        set_label (priv->label_count1, count[0]);
        set_label (priv->label_total, count[0] + count[1] + count[2] + count[3] + count[4]);
 }


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