From c1e0f6e17c16236d68ed26a5e413e9234293f4d9 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Thu, 16 Jan 2014 01:40:47 -0200 Subject: [PATCH] BUGFIX: Fix crash when adding meters and toggling detailed CPU time. See https://bugzilla.redhat.com/show_bug.cgi?id=987805 for details. (thanks to Dawid Gajownik for the detailed analysis!) --- BatteryMeter.c | 1 - CPUMeter.c | 8 +------- ChangeLog | 2 ++ ClockMeter.c | 1 - HostnameMeter.c | 1 - LoadAverageMeter.c | 3 +-- MemoryMeter.c | 2 +- Meter.c | 16 +++++++++++----- Meter.h | 7 ++++--- SwapMeter.c | 1 - TasksMeter.c | 1 - UptimeMeter.c | 1 - 12 files changed, 20 insertions(+), 24 deletions(-) diff --git a/BatteryMeter.c b/BatteryMeter.c index d2a4049d..362756c0 100644 --- a/BatteryMeter.c +++ b/BatteryMeter.c @@ -335,7 +335,6 @@ MeterClass BatteryMeter_class = { }, .setValues = BatteryMeter_setValues, .defaultMode = TEXT_METERMODE, - .items = 1, .total = 100.0, .attributes = BatteryMeter_attributes, .name = "Battery", diff --git a/CPUMeter.c b/CPUMeter.c index 64286f2f..77e7399b 100644 --- a/CPUMeter.c +++ b/CPUMeter.c @@ -228,7 +228,7 @@ MeterClass CPUMeter_class = { }, .setValues = CPUMeter_setValues, .defaultMode = BAR_METERMODE, - .items = 8, + .maxItems = 8, .total = 100.0, .attributes = CPUMeter_attributes, .name = "CPU", @@ -244,7 +244,6 @@ MeterClass AllCPUsMeter_class = { .display = CPUMeter_display }, .defaultMode = CUSTOM_METERMODE, - .items = 1, .total = 100.0, .attributes = CPUMeter_attributes, .name = "AllCPUs", @@ -263,7 +262,6 @@ MeterClass AllCPUs2Meter_class = { .display = CPUMeter_display }, .defaultMode = CUSTOM_METERMODE, - .items = 1, .total = 100.0, .attributes = CPUMeter_attributes, .name = "AllCPUs2", @@ -282,7 +280,6 @@ MeterClass LeftCPUsMeter_class = { .display = CPUMeter_display }, .defaultMode = CUSTOM_METERMODE, - .items = 1, .total = 100.0, .attributes = CPUMeter_attributes, .name = "LeftCPUs", @@ -301,7 +298,6 @@ MeterClass RightCPUsMeter_class = { .display = CPUMeter_display }, .defaultMode = CUSTOM_METERMODE, - .items = 1, .total = 100.0, .attributes = CPUMeter_attributes, .name = "RightCPUs", @@ -320,7 +316,6 @@ MeterClass LeftCPUs2Meter_class = { .display = CPUMeter_display }, .defaultMode = CUSTOM_METERMODE, - .items = 1, .total = 100.0, .attributes = CPUMeter_attributes, .name = "LeftCPUs2", @@ -339,7 +334,6 @@ MeterClass RightCPUs2Meter_class = { .display = CPUMeter_display }, .defaultMode = CUSTOM_METERMODE, - .items = 1, .total = 100.0, .attributes = CPUMeter_attributes, .name = "RightCPUs2", diff --git a/ChangeLog b/ChangeLog index e22d0bdc..424a8da4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -8,6 +8,8 @@ What's new in version 1.0.3 (thanks to Jann Horn) * Further performance improvements due to conditional parsing of IO data depending on selected fields. +* BUGFIX: Fix crash when adding meters and toggling detailed CPU time. + (thanks to Dawid Gajownik) What's new in version 1.0.2 diff --git a/ClockMeter.c b/ClockMeter.c index 3b0911fe..ffe79815 100644 --- a/ClockMeter.c +++ b/ClockMeter.c @@ -34,7 +34,6 @@ MeterClass ClockMeter_class = { .setValues = ClockMeter_setValues, .defaultMode = TEXT_METERMODE, .total = 100.0, - .items = 1, .attributes = ClockMeter_attributes, .name = "Clock", .uiName = "Clock", diff --git a/HostnameMeter.c b/HostnameMeter.c index 95ac9714..b46541c3 100644 --- a/HostnameMeter.c +++ b/HostnameMeter.c @@ -32,7 +32,6 @@ MeterClass HostnameMeter_class = { .setValues = HostnameMeter_setValues, .defaultMode = TEXT_METERMODE, .total = 100.0, - .items = 1, .attributes = HostnameMeter_attributes, .name = "Hostname", .uiName = "Hostname", diff --git a/LoadAverageMeter.c b/LoadAverageMeter.c index b8fbfa64..850f8029 100644 --- a/LoadAverageMeter.c +++ b/LoadAverageMeter.c @@ -74,7 +74,7 @@ MeterClass LoadAverageMeter_class = { }, .setValues = LoadAverageMeter_setValues, .defaultMode = TEXT_METERMODE, - .items = 3, + .maxItems = 3, .total = 100.0, .attributes = LoadAverageMeter_attributes, .name = "LoadAverage", @@ -90,7 +90,6 @@ MeterClass LoadMeter_class = { }, .setValues = LoadMeter_setValues, .defaultMode = TEXT_METERMODE, - .items = 1, .total = 100.0, .attributes = LoadMeter_attributes, .name = "Load", diff --git a/MemoryMeter.c b/MemoryMeter.c index 47f28af4..8d176b20 100644 --- a/MemoryMeter.c +++ b/MemoryMeter.c @@ -66,7 +66,7 @@ MeterClass MemoryMeter_class = { }, .setValues = MemoryMeter_setValues, .defaultMode = BAR_METERMODE, - .items = 3, + .maxItems = 3, .total = 100.0, .attributes = MemoryMeter_attributes, "Memory", diff --git a/Meter.c b/Meter.c index 7948028e..34238c86 100644 --- a/Meter.c +++ b/Meter.c @@ -52,12 +52,13 @@ typedef struct MeterClass_ { const Meter_Draw draw; const Meter_SetValues setValues; const int defaultMode; - int items; const double total; const int* attributes; const char* name; const char* uiName; const char* caption; + const char maxItems; + char curItems; } MeterClass; #define As_Meter(this_) ((MeterClass*)((this_)->super.klass)) @@ -70,8 +71,8 @@ typedef struct MeterClass_ { #define Meter_doneFn(this_) As_Meter(this_)->done #define Meter_setValues(this_, c_, i_) As_Meter(this_)->setValues((Meter*)(this_), c_, i_) #define Meter_defaultMode(this_) As_Meter(this_)->defaultMode -#define Meter_getItems(this_) As_Meter(this_)->items -#define Meter_setItems(this_, n_) As_Meter(this_)->items = (n_) +#define Meter_getItems(this_) As_Meter(this_)->curItems +#define Meter_setItems(this_, n_) As_Meter(this_)->curItems = (n_) #define Meter_attributes(this_) As_Meter(this_)->attributes #define Meter_name(this_) As_Meter(this_)->name #define Meter_uiName(this_) As_Meter(this_)->uiName @@ -146,12 +147,17 @@ MeterClass* Meter_types[] = { }; Meter* Meter_new(ProcessList* pl, int param, MeterClass* type) { - Meter* this = calloc(sizeof(Meter), 1); + Meter* this = calloc(1, sizeof(Meter)); Object_setClass(this, type); this->h = 1; this->param = param; this->pl = pl; - this->values = calloc(sizeof(double), type->items); + char maxItems = type->maxItems; + if (maxItems == 0) { + maxItems = 1; + } + type->curItems = maxItems; + this->values = calloc(maxItems, sizeof(double)); this->total = type->total; this->caption = strdup(type->caption); if (Meter_initFn(this)) diff --git a/Meter.h b/Meter.h index 64668c4b..a5c30b1e 100644 --- a/Meter.h +++ b/Meter.h @@ -30,12 +30,13 @@ typedef struct MeterClass_ { const Meter_Draw draw; const Meter_SetValues setValues; const int defaultMode; - int items; const double total; const int* attributes; const char* name; const char* uiName; const char* caption; + const char maxItems; + char curItems; } MeterClass; #define As_Meter(this_) ((MeterClass*)((this_)->super.klass)) @@ -48,8 +49,8 @@ typedef struct MeterClass_ { #define Meter_doneFn(this_) As_Meter(this_)->done #define Meter_setValues(this_, c_, i_) As_Meter(this_)->setValues((Meter*)(this_), c_, i_) #define Meter_defaultMode(this_) As_Meter(this_)->defaultMode -#define Meter_getItems(this_) As_Meter(this_)->items -#define Meter_setItems(this_, n_) As_Meter(this_)->items = (n_) +#define Meter_getItems(this_) As_Meter(this_)->curItems +#define Meter_setItems(this_, n_) As_Meter(this_)->curItems = (n_) #define Meter_attributes(this_) As_Meter(this_)->attributes #define Meter_name(this_) As_Meter(this_)->name #define Meter_uiName(this_) As_Meter(this_)->uiName diff --git a/SwapMeter.c b/SwapMeter.c index ff229d3e..6bdf612c 100644 --- a/SwapMeter.c +++ b/SwapMeter.c @@ -66,7 +66,6 @@ MeterClass SwapMeter_class = { }, .setValues = SwapMeter_setValues, .defaultMode = BAR_METERMODE, - .items = 1, .total = 100.0, .attributes = SwapMeter_attributes, .name = "Swap", diff --git a/TasksMeter.c b/TasksMeter.c index b6b1aff9..22a125e5 100644 --- a/TasksMeter.c +++ b/TasksMeter.c @@ -63,7 +63,6 @@ MeterClass TasksMeter_class = { }, .setValues = TasksMeter_setValues, .defaultMode = TEXT_METERMODE, - .items = 1, .total = 100.0, .attributes = TasksMeter_attributes, .name = "Tasks", diff --git a/UptimeMeter.c b/UptimeMeter.c index 97d0777b..9e288171 100644 --- a/UptimeMeter.c +++ b/UptimeMeter.c @@ -56,7 +56,6 @@ MeterClass UptimeMeter_class = { }, .setValues = UptimeMeter_setValues, .defaultMode = TEXT_METERMODE, - .items = 1, .total = 100.0, .attributes = UptimeMeter_attributes, .name = "Uptime",