From e1ce141bc3123c1dea7eb2ef908c8ebce945c72a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= Date: Tue, 24 Nov 2020 18:31:03 +0100 Subject: [PATCH] Use size_t as len type for Meter_UpdateValues Most of the time the parameter is passed to snprintf type functions --- BatteryMeter.c | 2 +- CPUMeter.c | 2 +- ClockMeter.c | 2 +- DateMeter.c | 2 +- DateTimeMeter.c | 2 +- DiskIOMeter.c | 2 +- HostnameMeter.c | 2 +- LoadAverageMeter.c | 4 ++-- MemoryMeter.c | 2 +- Meter.c | 5 ++--- Meter.h | 4 ++-- NetworkIOMeter.c | 2 +- SwapMeter.c | 2 +- TasksMeter.c | 2 +- UptimeMeter.c | 2 +- linux/PressureStallMeter.c | 2 +- linux/SELinuxMeter.c | 2 +- linux/SystemdMeter.c | 2 +- linux/ZramMeter.c | 14 +++++++++----- zfs/ZfsArcMeter.c | 2 +- zfs/ZfsCompressedArcMeter.c | 4 ++-- 21 files changed, 33 insertions(+), 30 deletions(-) diff --git a/BatteryMeter.c b/BatteryMeter.c index 5a06825f..4836809b 100644 --- a/BatteryMeter.c +++ b/BatteryMeter.c @@ -21,7 +21,7 @@ static const int BatteryMeter_attributes[] = { BATTERY }; -static void BatteryMeter_updateValues(Meter* this, char* buffer, int len) { +static void BatteryMeter_updateValues(Meter* this, char* buffer, size_t len) { ACPresence isOnAC; double percent; diff --git a/CPUMeter.c b/CPUMeter.c index 855d94bd..97fc3f0b 100644 --- a/CPUMeter.c +++ b/CPUMeter.c @@ -50,7 +50,7 @@ static void CPUMeter_init(Meter* this) { Meter_setCaption(this, "Avg"); } -static void CPUMeter_updateValues(Meter* this, char* buffer, int size) { +static void CPUMeter_updateValues(Meter* this, char* buffer, size_t size) { int cpu = this->param; if (cpu > this->pl->cpuCount) { xSnprintf(buffer, size, "absent"); diff --git a/ClockMeter.c b/ClockMeter.c index 3cc9dd03..023ca425 100644 --- a/ClockMeter.c +++ b/ClockMeter.c @@ -19,7 +19,7 @@ static const int ClockMeter_attributes[] = { CLOCK }; -static void ClockMeter_updateValues(Meter* this, char* buffer, int size) { +static void ClockMeter_updateValues(Meter* this, char* buffer, size_t size) { time_t t = time(NULL); struct tm result; struct tm* lt = localtime_r(&t, &result); diff --git a/DateMeter.c b/DateMeter.c index bfe0cbf1..bd6a306b 100644 --- a/DateMeter.c +++ b/DateMeter.c @@ -19,7 +19,7 @@ static const int DateMeter_attributes[] = { DATE }; -static void DateMeter_updateValues(Meter* this, char* buffer, int size) { +static void DateMeter_updateValues(Meter* this, char* buffer, size_t size) { time_t t = time(NULL); struct tm result; struct tm* lt = localtime_r(&t, &result); diff --git a/DateTimeMeter.c b/DateTimeMeter.c index ad5e81ea..0d231cd5 100644 --- a/DateTimeMeter.c +++ b/DateTimeMeter.c @@ -19,7 +19,7 @@ static const int DateTimeMeter_attributes[] = { DATETIME }; -static void DateTimeMeter_updateValues(Meter* this, char* buffer, int size) { +static void DateTimeMeter_updateValues(Meter* this, char* buffer, size_t size) { time_t t = time(NULL); struct tm result; struct tm* lt = localtime_r(&t, &result); diff --git a/DiskIOMeter.c b/DiskIOMeter.c index da6ae7d5..0105ce35 100644 --- a/DiskIOMeter.c +++ b/DiskIOMeter.c @@ -30,7 +30,7 @@ static unsigned long int cached_read_diff = 0; static unsigned long int cached_write_diff = 0; static double cached_utilisation_diff = 0.0; -static void DiskIOMeter_updateValues(Meter* this, char* buffer, int len) { +static void DiskIOMeter_updateValues(Meter* this, char* buffer, size_t len) { static unsigned long long int cached_last_update = 0; struct timeval tv; diff --git a/HostnameMeter.c b/HostnameMeter.c index 000bc3fd..af8e3493 100644 --- a/HostnameMeter.c +++ b/HostnameMeter.c @@ -19,7 +19,7 @@ static const int HostnameMeter_attributes[] = { HOSTNAME }; -static void HostnameMeter_updateValues(Meter* this, char* buffer, int size) { +static void HostnameMeter_updateValues(Meter* this, char* buffer, size_t size) { (void) this; gethostname(buffer, size - 1); } diff --git a/LoadAverageMeter.c b/LoadAverageMeter.c index 76b89ea1..d5424cd2 100644 --- a/LoadAverageMeter.c +++ b/LoadAverageMeter.c @@ -24,7 +24,7 @@ static const int LoadMeter_attributes[] = { LOAD }; -static void LoadAverageMeter_updateValues(Meter* this, char* buffer, int size) { +static void LoadAverageMeter_updateValues(Meter* this, char* buffer, size_t size) { Platform_getLoadAverage(&this->values[0], &this->values[1], &this->values[2]); xSnprintf(buffer, size, "%.2f/%.2f/%.2f", this->values[0], this->values[1], this->values[2]); } @@ -40,7 +40,7 @@ static void LoadAverageMeter_display(const Object* cast, RichString* out) { RichString_append(out, CRT_colors[LOAD_AVERAGE_FIFTEEN], buffer); } -static void LoadMeter_updateValues(Meter* this, char* buffer, int size) { +static void LoadMeter_updateValues(Meter* this, char* buffer, size_t size) { double five, fifteen; Platform_getLoadAverage(&this->values[0], &five, &fifteen); if (this->values[0] > this->total) { diff --git a/MemoryMeter.c b/MemoryMeter.c index 3d29ddf4..5e334a93 100644 --- a/MemoryMeter.c +++ b/MemoryMeter.c @@ -19,7 +19,7 @@ static const int MemoryMeter_attributes[] = { MEMORY_CACHE }; -static void MemoryMeter_updateValues(Meter* this, char* buffer, int size) { +static void MemoryMeter_updateValues(Meter* this, char* buffer, size_t size) { int written; Platform_setMemoryValues(this); diff --git a/Meter.c b/Meter.c index 71457d4d..9c9a8dea 100644 --- a/Meter.c +++ b/Meter.c @@ -49,7 +49,7 @@ Meter* Meter_new(const struct ProcessList_* pl, int param, const MeterClass* typ return this; } -int Meter_humanUnit(char* buffer, unsigned long int value, int size) { +int Meter_humanUnit(char* buffer, unsigned long int value, size_t size) { const char* prefix = "KMGTPEZY"; unsigned long int powi = 1; unsigned int powj = 1, precision = 2; @@ -441,8 +441,7 @@ const MeterMode* const Meter_modes[] = { /* Blank meter */ -static void BlankMeter_updateValues(Meter* this, char* buffer, int size) { - (void) this; (void) buffer; (void) size; +static void BlankMeter_updateValues(ATTR_UNUSED Meter* this, char* buffer, size_t size) { if (size > 0) { *buffer = 0; } diff --git a/Meter.h b/Meter.h index 43518e52..13cbebba 100644 --- a/Meter.h +++ b/Meter.h @@ -26,7 +26,7 @@ typedef struct Meter_ Meter; typedef void(*Meter_Init)(Meter*); typedef void(*Meter_Done)(Meter*); typedef void(*Meter_UpdateMode)(Meter*, int); -typedef void(*Meter_UpdateValues)(Meter*, char*, int); +typedef void(*Meter_UpdateValues)(Meter*, char*, size_t); typedef void(*Meter_Draw)(Meter*, int, int, int); typedef struct MeterClass_ { @@ -101,7 +101,7 @@ extern const MeterClass Meter_class; Meter* Meter_new(const ProcessList* pl, int param, const MeterClass* type); -int Meter_humanUnit(char* buffer, unsigned long int value, int size); +int Meter_humanUnit(char* buffer, unsigned long int value, size_t size); void Meter_delete(Object* cast); diff --git a/NetworkIOMeter.c b/NetworkIOMeter.c index a28738a2..90ec9909 100644 --- a/NetworkIOMeter.c +++ b/NetworkIOMeter.c @@ -24,7 +24,7 @@ static unsigned long int cached_rxp_diff = 0; static unsigned long int cached_txb_diff = 0; static unsigned long int cached_txp_diff = 0; -static void NetworkIOMeter_updateValues(ATTR_UNUSED Meter* this, char* buffer, int len) { +static void NetworkIOMeter_updateValues(ATTR_UNUSED Meter* this, char* buffer, size_t len) { static unsigned long long int cached_last_update = 0; struct timeval tv; diff --git a/SwapMeter.c b/SwapMeter.c index 9de7fa35..0f8bb5a4 100644 --- a/SwapMeter.c +++ b/SwapMeter.c @@ -17,7 +17,7 @@ static const int SwapMeter_attributes[] = { SWAP }; -static void SwapMeter_updateValues(Meter* this, char* buffer, int size) { +static void SwapMeter_updateValues(Meter* this, char* buffer, size_t size) { int written; Platform_setSwapValues(this); diff --git a/TasksMeter.c b/TasksMeter.c index 562cc8fc..7696988a 100644 --- a/TasksMeter.c +++ b/TasksMeter.c @@ -23,7 +23,7 @@ static const int TasksMeter_attributes[] = { TASKS_RUNNING }; -static void TasksMeter_updateValues(Meter* this, char* buffer, int len) { +static void TasksMeter_updateValues(Meter* this, char* buffer, size_t len) { const ProcessList* pl = this->pl; this->values[0] = pl->kernelThreads; this->values[1] = pl->userlandThreads; diff --git a/UptimeMeter.c b/UptimeMeter.c index 79070286..37740c21 100644 --- a/UptimeMeter.c +++ b/UptimeMeter.c @@ -17,7 +17,7 @@ static const int UptimeMeter_attributes[] = { UPTIME }; -static void UptimeMeter_updateValues(Meter* this, char* buffer, int len) { +static void UptimeMeter_updateValues(Meter* this, char* buffer, size_t len) { int totalseconds = Platform_getUptime(); if (totalseconds == -1) { xSnprintf(buffer, len, "(unknown)"); diff --git a/linux/PressureStallMeter.c b/linux/PressureStallMeter.c index d2fd7d37..745068c9 100644 --- a/linux/PressureStallMeter.c +++ b/linux/PressureStallMeter.c @@ -25,7 +25,7 @@ static const int PressureStallMeter_attributes[] = { PRESSURE_STALL_THREEHUNDRED }; -static void PressureStallMeter_updateValues(Meter* this, char* buffer, int len) { +static void PressureStallMeter_updateValues(Meter* this, char* buffer, size_t len) { const char* file; if (strstr(Meter_name(this), "CPU")) { file = "cpu"; diff --git a/linux/SELinuxMeter.c b/linux/SELinuxMeter.c index a8b07bd3..64a3f2a7 100644 --- a/linux/SELinuxMeter.c +++ b/linux/SELinuxMeter.c @@ -70,7 +70,7 @@ static bool isSelinuxEnforcing(void) { return !!enforce; } -static void SELinuxMeter_updateValues(ATTR_UNUSED Meter* this, char* buffer, int len) { +static void SELinuxMeter_updateValues(ATTR_UNUSED Meter* this, char* buffer, size_t len) { enabled = isSelinuxEnabled(); enforcing = isSelinuxEnforcing(); diff --git a/linux/SystemdMeter.c b/linux/SystemdMeter.c index d49185ee..4350d264 100644 --- a/linux/SystemdMeter.c +++ b/linux/SystemdMeter.c @@ -229,7 +229,7 @@ static void updateViaExec(void) { fclose(commandOutput); } -static void SystemdMeter_updateValues(ATTR_UNUSED Meter* this, char* buffer, int size) { +static void SystemdMeter_updateValues(ATTR_UNUSED Meter* this, char* buffer, size_t size) { free(systemState); systemState = NULL; nFailedUnits = nInstalledJobs = nNames = nJobs = INVALID_VALUE; diff --git a/linux/ZramMeter.c b/linux/ZramMeter.c index cee70586..35148871 100644 --- a/linux/ZramMeter.c +++ b/linux/ZramMeter.c @@ -11,7 +11,7 @@ static const int ZramMeter_attributes[] = { ZRAM }; -static void ZramMeter_updateValues(Meter* this, char* buffer, int size) { +static void ZramMeter_updateValues(Meter* this, char* buffer, size_t size) { int written; Platform_setZramValues(this); @@ -38,11 +38,15 @@ static void ZramMeter_updateValues(Meter* this, char* buffer, int size) { } *buffer++ = ')'; size--; - if ((size -= written) > 0) { - *buffer++ = '/'; - size--; - Meter_humanUnit(buffer, this->total, size); + if (size <= 0) { + return; } + *buffer++ = '/'; + size--; + if (size <= 0) { + return; + } + Meter_humanUnit(buffer, this->total, size); } static void ZramMeter_display(const Object* cast, RichString* out) { diff --git a/zfs/ZfsArcMeter.c b/zfs/ZfsArcMeter.c index 9bfc44b7..5b8082bb 100644 --- a/zfs/ZfsArcMeter.c +++ b/zfs/ZfsArcMeter.c @@ -33,7 +33,7 @@ void ZfsArcMeter_readStats(Meter* this, const ZfsArcStats* stats) { this->values[5] = stats->size; } -static void ZfsArcMeter_updateValues(Meter* this, char* buffer, int size) { +static void ZfsArcMeter_updateValues(Meter* this, char* buffer, size_t size) { int written; Platform_setZfsArcValues(this); diff --git a/zfs/ZfsCompressedArcMeter.c b/zfs/ZfsCompressedArcMeter.c index 31773664..8766f801 100644 --- a/zfs/ZfsCompressedArcMeter.c +++ b/zfs/ZfsCompressedArcMeter.c @@ -32,11 +32,11 @@ void ZfsCompressedArcMeter_readStats(Meter* this, const ZfsArcStats* stats) { } } -static void ZfsCompressedArcMeter_printRatioString(const Meter* this, char* buffer, int size) { +static void ZfsCompressedArcMeter_printRatioString(const Meter* this, char* buffer, size_t size) { xSnprintf(buffer, size, "%.2f:1", this->total / this->values[0]); } -static void ZfsCompressedArcMeter_updateValues(Meter* this, char* buffer, int size) { +static void ZfsCompressedArcMeter_updateValues(Meter* this, char* buffer, size_t size) { Platform_setZfsCompressedArcValues(this); ZfsCompressedArcMeter_printRatioString(this, buffer, size);