OSDN Git Service

Dont allow zero division to happen
authorwifiextender <router@archlinux.info>
Sat, 20 Aug 2016 04:29:49 +0000 (06:29 +0200)
committerwifiextender <router@archlinux.info>
Sat, 20 Aug 2016 04:29:49 +0000 (06:29 +0200)
src/cpu.c
src/linux_functions.c
src/sound.c

index e87b425..90c02dd 100644 (file)
--- a/src/cpu.c
+++ b/src/cpu.c
@@ -80,7 +80,10 @@ get_cpu(char *str1) {
   previous_total = total;
   previous_idle  = cpu_active[IDLE_NUM];
 
-  percent        = ((uintmax_t)TICKZ * (diff_total - diff_idle)) / diff_total;
+  percent        = 0;
+  if (0 != diff_total) {
+    percent      = ((uintmax_t)TICKZ * (diff_total - diff_idle)) / diff_total;
+  }
 
   FILL_UINT_ARR(str1, percent);
 }
@@ -165,8 +168,11 @@ get_cores_load(char *str1) {
     previous_total[x] = total[x];
     previous_idle[x]  = core_active[x][IDLE_NUM];
 
-    percent[x]        = ((uintmax_t)TICKZ * (diff_total[x] - diff_idle[x]))
+    percent[x]        = 0;
+    if (0 != diff_total[x]) {
+      percent[x]      = ((uintmax_t)TICKZ * (diff_total[x] - diff_idle[x]))
                             / diff_total[x];
+    }
 
     GLUE2(all, FMT_UINT"%% ", percent[x]);
   }
@@ -211,7 +217,7 @@ get_cpu_temp(char *str1) {
     FILL_UINT_ARR(str1, temp2 / 1000);
   } else {
     FILL_UINT_ARR(str1, ((999 < temp2) ?
-      temp2 / 100 : temp2/10)); /* > 9C || < 9C */
+      temp2 / 100 : temp2 / 10)); /* > 9C || < 9C */
   }
 }
 
index bc21d58..3e7d243 100644 (file)
@@ -217,6 +217,9 @@ get_battery(char *str1) {
   OPEN_X(fp, temp, FMT_UINT, &used);
 #pragma GCC diagnostic pop
 
-  percent = (used * 100) / total;
+  percent = 0;
+  if (0 != total) {
+    percent = (used * 100) / total;
+  }
   FILL_UINT_ARR(str1, percent);
 }
index 07da4d7..b34f07e 100644 (file)
@@ -73,7 +73,10 @@ get_volume(char *str1) {
   }
   snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
 
-  percent = (vol * 100) / max;
+  percent = 0L;
+  if (0 != max) {
+    percent = (vol * 100) / max;
+  }
 
   snd_mixer_selem_id_free(s_elem);
   snd_mixer_close(handle);