[gnome-software/gnome-3-36] trivial: Fix unlikely division by zero



commit 69cf2109d77b45406704ef62cab5ab3dcb46fc4a
Author: Richard Hughes <richard hughsie com>
Date:   Fri Feb 7 13:56:31 2020 +0000

    trivial: Fix unlikely division by zero
    
    Spotted by Coverity.

 lib/gs-plugin-loader.c | 7 +++++--
 lib/gs-utils.c         | 4 +++-
 2 files changed, 8 insertions(+), 3 deletions(-)
---
diff --git a/lib/gs-plugin-loader.c b/lib/gs-plugin-loader.c
index c8136f1c..1fe4bfb9 100644
--- a/lib/gs-plugin-loader.c
+++ b/lib/gs-plugin-loader.c
@@ -2741,8 +2741,11 @@ gs_plugin_loader_settings_changed_cb (GSettings *settings,
 static gint
 get_max_parallel_ops (void)
 {
-       /* We're allowing 1 op per GB of memory */
-       return (gint) MAX (round((gdouble) gs_utils_get_memory_total () / 1024), 1.0);
+       guint mem_total = gs_utils_get_memory_total ();
+       if (mem_total == 0)
+               return 8;
+       /* allow 1 op per GB of memory */
+       return (gint) MAX (round((gdouble) mem_total / 1024), 1.0);
 }
 
 static void
diff --git a/lib/gs-utils.c b/lib/gs-utils.c
index c7ded067..8376fc5a 100644
--- a/lib/gs-utils.c
+++ b/lib/gs-utils.c
@@ -1129,7 +1129,9 @@ gs_utils_get_memory_total (void)
 #if defined(__linux__)
        struct sysinfo si = { 0 };
        sysinfo (&si);
-       return si.totalram / MB_IN_BYTES / si.mem_unit;
+       if (si.mem_unit > 0)
+               return si.totalram / MB_IN_BYTES / si.mem_unit;
+       return 0;
 #elif defined(__FreeBSD__)
        unsigned long physmem;
        sysctl ((int[]){ CTL_HW, HW_PHYSMEM }, 2, &physmem, &(size_t){ sizeof (physmem) }, NULL, 0);


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