From 7d3f67e8223168f1da4f82f928029056f74ad651 Mon Sep 17 00:00:00 2001 From: Explorer09 Date: Fri, 11 Mar 2016 10:43:31 +0800 Subject: [PATCH 1/2] Revert 5c593fae4227651075cfc9445f17dad6ae0a0b47 (xCalloc) calloc() allows 'nmemb' or 'size' to be zero, in which case NULL may be returned. Letting htop die because of either argument being zero doesn't make sense. As a side note: As size_t is unsigned, compiler should be able to optimize conditional (nmemb > 0 && size > 0) to (nmemb && size). This theorically shouldn't increase code size too much. --- XAlloc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/XAlloc.c b/XAlloc.c index cc290211..38556987 100644 --- a/XAlloc.c +++ b/XAlloc.c @@ -29,7 +29,7 @@ void* xMalloc(size_t size) { void* xCalloc(size_t nmemb, size_t size) { void* data = calloc(nmemb, size); - if (!data) { + if (!data && nmemb > 0 && size > 0) { fail(); } return data; From 99fb3070a2bcbb8cc2956c360f5922bfde6e233e Mon Sep 17 00:00:00 2001 From: Explorer09 Date: Fri, 11 Mar 2016 10:54:34 +0800 Subject: [PATCH 2/2] Explicit "maxItems" property of meters Two changes in this commit: - All meters now explicitly specify "maxItems" property, even for just 1 item. (Exception is "container" CPU meter classes, which use CUSTOM_METERMODE.) - "maxItems" being 0 is now allowed. This will let bar meters and graph meters render an empty meter. --- BatteryMeter.c | 1 + CPUMeter.c | 2 +- ClockMeter.c | 1 + HostnameMeter.c | 1 + LoadAverageMeter.c | 1 + Meter.c | 9 +++------ SwapMeter.c | 1 + TasksMeter.c | 2 +- UptimeMeter.c | 1 + 9 files changed, 11 insertions(+), 8 deletions(-) diff --git a/BatteryMeter.c b/BatteryMeter.c index 5fda38b1..622b7798 100644 --- a/BatteryMeter.c +++ b/BatteryMeter.c @@ -75,6 +75,7 @@ MeterClass BatteryMeter_class = { }, .setValues = BatteryMeter_setValues, .defaultMode = TEXT_METERMODE, + .maxItems = 1, .total = 100.0, .attributes = BatteryMeter_attributes, .name = "Battery", diff --git a/CPUMeter.c b/CPUMeter.c index 7685f405..4a945c0e 100644 --- a/CPUMeter.c +++ b/CPUMeter.c @@ -312,8 +312,8 @@ MeterClass LeftCPUs2Meter_class = { .total = 100.0, .attributes = CPUMeter_attributes, .name = "LeftCPUs2", - .description = "CPUs (1&2/4): first half in 2 shorter columns", .uiName = "CPUs (1&2/4)", + .description = "CPUs (1&2/4): first half in 2 shorter columns", .caption = "CPU", .draw = DualColCPUsMeter_draw, .init = AllCPUsMeter_init, diff --git a/ClockMeter.c b/ClockMeter.c index d702aa5f..a248b9bb 100644 --- a/ClockMeter.c +++ b/ClockMeter.c @@ -34,6 +34,7 @@ MeterClass ClockMeter_class = { }, .setValues = ClockMeter_setValues, .defaultMode = TEXT_METERMODE, + .maxItems = 1, .total = 1440, /* 24*60 */ .attributes = ClockMeter_attributes, .name = "Clock", diff --git a/HostnameMeter.c b/HostnameMeter.c index b46541c3..ccfaaca9 100644 --- a/HostnameMeter.c +++ b/HostnameMeter.c @@ -31,6 +31,7 @@ MeterClass HostnameMeter_class = { }, .setValues = HostnameMeter_setValues, .defaultMode = TEXT_METERMODE, + .maxItems = 0, .total = 100.0, .attributes = HostnameMeter_attributes, .name = "Hostname", diff --git a/LoadAverageMeter.c b/LoadAverageMeter.c index d292a7cb..a5a04d32 100644 --- a/LoadAverageMeter.c +++ b/LoadAverageMeter.c @@ -77,6 +77,7 @@ MeterClass LoadMeter_class = { }, .setValues = LoadMeter_setValues, .defaultMode = TEXT_METERMODE, + .maxItems = 1, .total = 100.0, .attributes = LoadMeter_attributes, .name = "Load", diff --git a/Meter.c b/Meter.c index 9a7868e7..321fd28b 100644 --- a/Meter.c +++ b/Meter.c @@ -132,12 +132,8 @@ Meter* Meter_new(struct ProcessList_* pl, int param, MeterClass* type) { this->h = 1; this->param = param; this->pl = pl; - char maxItems = type->maxItems; - if (maxItems == 0) { - maxItems = 1; - } - type->curItems = maxItems; - this->values = xCalloc(maxItems, sizeof(double)); + type->curItems = type->maxItems; + this->values = xCalloc(type->maxItems, sizeof(double)); this->total = type->total; this->caption = xStrdup(type->caption); if (Meter_initFn(this)) @@ -550,6 +546,7 @@ MeterClass BlankMeter_class = { }, .setValues = BlankMeter_setValues, .defaultMode = TEXT_METERMODE, + .maxItems = 0, .total = 100.0, .attributes = BlankMeter_attributes, .name = "Blank", diff --git a/SwapMeter.c b/SwapMeter.c index 895ca0be..476c4240 100644 --- a/SwapMeter.c +++ b/SwapMeter.c @@ -56,6 +56,7 @@ MeterClass SwapMeter_class = { }, .setValues = SwapMeter_setValues, .defaultMode = BAR_METERMODE, + .maxItems = 1, .total = 100.0, .attributes = SwapMeter_attributes, .name = "Swap", diff --git a/TasksMeter.c b/TasksMeter.c index f3764d64..dc057188 100644 --- a/TasksMeter.c +++ b/TasksMeter.c @@ -74,8 +74,8 @@ MeterClass TasksMeter_class = { }, .setValues = TasksMeter_setValues, .defaultMode = TEXT_METERMODE, - .total = 100.0, .maxItems = 4, + .total = 100.0, .attributes = TasksMeter_attributes, .name = "Tasks", .uiName = "Task counter", diff --git a/UptimeMeter.c b/UptimeMeter.c index a033b171..5e0a9e9a 100644 --- a/UptimeMeter.c +++ b/UptimeMeter.c @@ -51,6 +51,7 @@ MeterClass UptimeMeter_class = { }, .setValues = UptimeMeter_setValues, .defaultMode = TEXT_METERMODE, + .maxItems = 1, .total = 100.0, .attributes = UptimeMeter_attributes, .name = "Uptime",