[gnome-software/wip/hughsie/covscan: 22/23] trivial: Fix unlikely division by zero



commit 19608e3f21d372a246243c3a8b04544172babe97
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 f0e124cb..f5e144e9 100644
--- a/lib/gs-plugin-loader.c
+++ b/lib/gs-plugin-loader.c
@@ -2738,8 +2738,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]