some fixes for update_stat():
authorJohannes Winkelmann <jw@tks6.net>
Sat, 27 Aug 2005 14:55:10 +0000 (14:55 +0000)
committerJohannes Winkelmann <jw@tks6.net>
Sat, 27 Aug 2005 14:55:10 +0000 (14:55 +0000)
- initialize cpu_info struct properly (a large conkyrc could pollute that memory area)
- allocate info.cpu_usage to cpu_count + 1

git-svn-id: https://conky.svn.sourceforge.net/svnroot/conky/trunk/conky@220 7f574dfc-610e-0410-a909-a81674777703

src/linux.c

index 3be3675..2bcf7ed 100644 (file)
@@ -320,13 +320,14 @@ void update_total_processes()
        update_sysinfo();
 }
 
+#define CPU_SAMPLE_COUNT 15
 struct cpu_info {
        unsigned int cpu_user;
        unsigned int cpu_system;
        unsigned int cpu_nice;
        double last_cpu_sum;
        int clock_ticks;
-       double cpu_val[15];
+       double cpu_val[CPU_SAMPLE_COUNT];
 };
 static short cpu_setup = 0;
 static int rep;
@@ -354,7 +355,7 @@ void get_cpu_count()
                        info.cpu_count++;
                }
        }
-       info.cpu_usage = malloc(info.cpu_count * sizeof(float));
+       info.cpu_usage = malloc((info.cpu_count + 1) * sizeof(float));
 }
 
 
@@ -371,6 +372,13 @@ inline static void update_stat()
        }
        if (cpu == NULL) {
                cpu = malloc((info.cpu_count + 1) * sizeof(struct cpu_info));
+               for (index = 0; index < info.cpu_count + 1; ++index) {
+                       cpu[index].clock_ticks = 0;
+                       cpu[index].last_cpu_sum = 0;
+                       for (i = 0; i < CPU_SAMPLE_COUNT; ++i) {
+                               cpu[index].cpu_val[i] = 0;
+                       }
+               }
        }
        if (stat_fp == NULL) {
                stat_fp = open_file("/proc/stat", &rep);