From 9ed9d73ab548ef81328c97686f4b01dee107392e Mon Sep 17 00:00:00 2001 From: Explorer09 Date: Wed, 20 Apr 2022 13:05:35 +0800 Subject: Correct titleBuffer size and share it in alignedProcessFieldTitle() * The size of titleBuffer should be 257 bytes, not 256. * Remove redundant `static char titleBuffer[]` delarations within `alignedProcessFieldTitle()` and let the subroutine use one shared buffer for printing field title. This reduces code size. Signed-off-by: Kang-Che Sung --- ProcessList.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/ProcessList.c b/ProcessList.c index 705786f6..fcc77a5f 100644 --- a/ProcessList.c +++ b/ProcessList.c @@ -82,40 +82,41 @@ void ProcessList_setPanel(ProcessList* this, Panel* panel) { this->panel = panel; } -static const char* alignedDynamicColumnTitle(const ProcessList* this, int key) { +static const char* alignedDynamicColumnTitle(const ProcessList* this, int key, char* titleBuffer, size_t titleBufferSize) { const DynamicColumn* column = Hashtable_get(this->dynamicColumns, key); if (column == NULL) return "- "; - static char titleBuffer[DYNAMIC_MAX_COLUMN_WIDTH + /* space */ 1 + /* null terminator */ + 1]; int width = column->width; if (!width || abs(width) > DYNAMIC_MAX_COLUMN_WIDTH) width = DYNAMIC_DEFAULT_COLUMN_WIDTH; - xSnprintf(titleBuffer, sizeof(titleBuffer), "%*s", width, column->heading); + xSnprintf(titleBuffer, titleBufferSize, "%*s", width, column->heading); return titleBuffer; } static const char* alignedProcessFieldTitle(const ProcessList* this, ProcessField field) { + static char titleBuffer[UINT8_MAX + sizeof(" ")]; + assert(sizeof(titleBuffer) >= DYNAMIC_MAX_COLUMN_WIDTH + sizeof(" ")); + assert(sizeof(titleBuffer) >= PROCESS_MAX_PID_DIGITS + sizeof(" ")); + assert(sizeof(titleBuffer) >= PROCESS_MAX_UID_DIGITS + sizeof(" ")); + if (field >= LAST_PROCESSFIELD) - return alignedDynamicColumnTitle(this, field); + return alignedDynamicColumnTitle(this, field, titleBuffer, sizeof(titleBuffer)); const char* title = Process_fields[field].title; if (!title) return "- "; if (Process_fields[field].pidColumn) { - static char titleBuffer[PROCESS_MAX_PID_DIGITS + sizeof(" ")]; xSnprintf(titleBuffer, sizeof(titleBuffer), "%*s ", Process_pidDigits, title); return titleBuffer; } if (field == ST_UID) { - static char titleBuffer[PROCESS_MAX_UID_DIGITS + sizeof(" ")]; xSnprintf(titleBuffer, sizeof(titleBuffer), "%*s ", Process_uidDigits, title); return titleBuffer; } if (Process_fields[field].autoWidth) { - static char titleBuffer[UINT8_MAX + 1]; if (field == PERCENT_CPU) xSnprintf(titleBuffer, sizeof(titleBuffer), "%*s ", Process_fieldWidths[field], title); else -- cgit v1.2.3