From dbf5583c5be30e712168127117db6bf5f30adba0 Mon Sep 17 00:00:00 2001 From: Jim Klimov Date: Wed, 25 Nov 2020 18:37:14 +0100 Subject: [PATCH 1/7] drivers/rhino.c: avoid variable-length arrays, their support became optional after C99 --- drivers/rhino.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/drivers/rhino.c b/drivers/rhino.c index 0d6c81db8b..bb0462a252 100644 --- a/drivers/rhino.c +++ b/drivers/rhino.c @@ -469,8 +469,14 @@ CommReceive(const unsigned char *bufptr, int size) static int send_command( int cmd ) { - int i, chk, checksum = 0, iend = 18, sizes = 19, ret, kount; /*, j, uc; */ - unsigned char ch, psend[sizes]; + static const size_t sizes = 19, iend = 18; + int i, chk, checksum = 0, ret, kount; /*, j, uc; */ + unsigned char ch, *psend = NULL; + + if ( !(psend = xmalloc(sizeof(char) * sizes)) ) { + upslogx(LOG_ERR, "send_command() failed to allocate buffer"); + return -1; + } /* mounting buffer to send */ @@ -510,6 +516,8 @@ send_command( int cmd ) usleep( UPSDELAY ); /* delay between sent command */ kount++; } + + free (psend); return ret; } From 1b4d818d8b8a9386c26742411199d1fdb3390c06 Mon Sep 17 00:00:00 2001 From: Jim Klimov Date: Wed, 25 Nov 2020 22:33:23 +0100 Subject: [PATCH 2/7] drivers/rhino.c: fix mismatched signedness in comparison --- drivers/rhino.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/rhino.c b/drivers/rhino.c index bb0462a252..15564e29f5 100644 --- a/drivers/rhino.c +++ b/drivers/rhino.c @@ -470,7 +470,8 @@ static int send_command( int cmd ) { static const size_t sizes = 19, iend = 18; - int i, chk, checksum = 0, ret, kount; /*, j, uc; */ + size_t i; + int chk, checksum = 0, ret, kount; /*, j, uc; */ unsigned char ch, *psend = NULL; if ( !(psend = xmalloc(sizeof(char) * sizes)) ) { From 8855c120bd35e02b008323599f2e1b3118088f47 Mon Sep 17 00:00:00 2001 From: Jim Klimov Date: Wed, 25 Nov 2020 18:37:51 +0100 Subject: [PATCH 3/7] drivers/rhino.c: sendshut(): handle negative returns from send_command() --- drivers/rhino.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/drivers/rhino.c b/drivers/rhino.c index 15564e29f5..4c3533b6e1 100644 --- a/drivers/rhino.c +++ b/drivers/rhino.c @@ -529,8 +529,10 @@ static void sendshut( void ) for(i=0; i < 30000; i++) usleep( UPSDELAY ); /* 15 seconds delay */ - send_command( CMD_SHUT ); - upslogx(LOG_NOTICE, "Ups shutdown command sent"); + if ( send_command( CMD_SHUT ) < 1 ) + upslogx(LOG_ERR, "Ups shutdown command sending failed"); + else + upslogx(LOG_NOTICE, "Ups shutdown command sent"); printf("Ups shutdown command sent\n"); } From cb395cae94fe5340bfe9129bbbb3433fbf8fee56 Mon Sep 17 00:00:00 2001 From: Jim Klimov Date: Wed, 25 Nov 2020 18:38:10 +0100 Subject: [PATCH 4/7] drivers/rhino.c: upsdrv_shutdown(): use sendshut() --- drivers/rhino.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/drivers/rhino.c b/drivers/rhino.c index 4c3533b6e1..cfc4beb674 100644 --- a/drivers/rhino.c +++ b/drivers/rhino.c @@ -748,13 +748,16 @@ void upsdrv_shutdown(void) if (!SourceFail) /* on line */ { + /* FIXME: Both legs of the if-clause send CMD_SHUT, where is the "forcing"? */ printf("On line, forcing shutdown command...\n"); - send_command( CMD_SHUT ); + /* send_command( CMD_SHUT ); */ + sendshut(); } else { printf("On battery, sending normal shutdown command...\n"); - send_command( CMD_SHUT ); + /* send_command( CMD_SHUT ); */ + sendshut(); } } From fb1b0908633e3878e6f4c29e62f724d4798564c4 Mon Sep 17 00:00:00 2001 From: Jim Klimov Date: Wed, 25 Nov 2020 18:46:29 +0100 Subject: [PATCH 5/7] drivers/apcsmart.c: avoid variable-length arrays, their support became optional after C99 --- drivers/apcsmart.c | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/drivers/apcsmart.c b/drivers/apcsmart.c index aadb22f84f..d05273b064 100644 --- a/drivers/apcsmart.c +++ b/drivers/apcsmart.c @@ -662,7 +662,7 @@ static void apc_flush(int flags) /* apc specific wrappers around set/del info - to handle "packed" variables */ static void apc_dstate_delinfo(apc_vartab_t *vt, int skip) { - char name[vt->nlen0], *nidx; + char *name, *nidx; int c; /* standard not packed var */ @@ -671,6 +671,11 @@ static void apc_dstate_delinfo(apc_vartab_t *vt, int skip) return; } + if ( !(name = xmalloc(sizeof(char) * vt->nlen0)) ) { + upslogx(LOG_ERR, "apc_dstate_delinfo() failed to allocate buffer"); + return; + } + strcpy(name, vt->name); nidx = strstr(name,".0.") + 1; @@ -680,12 +685,13 @@ static void apc_dstate_delinfo(apc_vartab_t *vt, int skip) } vt->cnt = 0; + free(name); } static void apc_dstate_setinfo(apc_vartab_t *vt, const char *upsval) { - char name[vt->nlen0], *nidx; - char temp[strlen(upsval) + 1], *vidx[APC_PACK_MAX], *com, *curr; + char *name, *nidx; + char *temp, *vidx[APC_PACK_MAX], *com, *curr; int c; /* standard not packed var */ @@ -694,6 +700,17 @@ static void apc_dstate_setinfo(apc_vartab_t *vt, const char *upsval) return; } + if ( !(name = xmalloc(sizeof(char) * vt->nlen0)) ) { + upslogx(LOG_ERR, "apc_dstate_setinfo() failed to allocate buffer"); + return; + } + + if ( !(temp = xmalloc(sizeof(char) * (strlen(upsval) + 1))) ) { + upslogx(LOG_ERR, "apc_dstate_setinfo() failed to allocate buffer"); + free(name); + return; + } + /* we have to set proper name for dstate_setinfo() calls */ strcpy(name, vt->name); nidx = strstr(name,".0.") + 1; @@ -734,6 +751,9 @@ static void apc_dstate_setinfo(apc_vartab_t *vt, const char *upsval) else dstate_setinfo(name, "N/A"); } + + free(name); + free(temp); } static const char *preread_data(apc_vartab_t *vt) From 8c0843c1efd0e38533f0d570318211ca1edcd10c Mon Sep 17 00:00:00 2001 From: Jim Klimov Date: Wed, 25 Nov 2020 18:57:22 +0100 Subject: [PATCH 6/7] drivers/nutdrv_qx.c: avoid variable-length arrays, their support became optional after C99 --- drivers/nutdrv_qx.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/drivers/nutdrv_qx.c b/drivers/nutdrv_qx.c index 55ed3c1e5e..8daedd2cc4 100644 --- a/drivers/nutdrv_qx.c +++ b/drivers/nutdrv_qx.c @@ -2778,20 +2778,29 @@ static int qx_process_answer(item_t *item, const int len) /* See header file for details. */ int qx_process(item_t *item, const char *command) { - char buf[sizeof(item->answer) - 1] = "", - cmd[command ? (strlen(command) >= SMALLBUF ? strlen(command) + 1 : SMALLBUF) : (item->command && strlen(item->command) >= SMALLBUF ? strlen(item->command) + 1 : SMALLBUF)]; + char buf[sizeof(item->answer) - 1] = "", *cmd; int len; + size_t cmdlen = command ? + (strlen(command) >= SMALLBUF ? strlen(command) + 1 : SMALLBUF) : + (item->command && strlen(item->command) >= SMALLBUF ? strlen(item->command) + 1 : SMALLBUF); + size_t cmdsz = (sizeof(char) * cmdlen); /* in bytes, to be pedantic */ + + if ( !(cmd = xmalloc(cmdsz)) ) { + upslogx(LOG_ERR, "qx_process() failed to allocate buffer"); + return -1; + } /* Prepare the command to be used */ - memset(cmd, 0, sizeof(cmd)); - snprintf(cmd, sizeof(cmd), "%s", command ? command : item->command); + memset(cmd, 0, cmdsz); + snprintf(cmd, cmdsz, "%s", command ? command : item->command); /* Preprocess the command */ if ( item->preprocess_command != NULL && - item->preprocess_command(item, cmd, sizeof(cmd)) == -1 + item->preprocess_command(item, cmd, cmdsz) == -1 ) { upsdebugx(4, "%s: failed to preprocess command [%s]", __func__, item->info_type); + free (cmd); return -1; } @@ -2808,10 +2817,13 @@ int qx_process(item_t *item, const char *command) upsdebugx(4, "%s: failed to preprocess answer [%s]", __func__, item->info_type); /* Clear answer, preventing it from being reused by next items with same command */ memset(item->answer, 0, sizeof(item->answer)); + free (cmd); return -1; } } + free (cmd); + /* Process the answer to get the value */ return qx_process_answer(item, len); } From f0167e6ff237c5db74bd0972b69857a58a278631 Mon Sep 17 00:00:00 2001 From: Jim Klimov Date: Wed, 25 Nov 2020 20:55:26 +0100 Subject: [PATCH 7/7] drivers/solis.c: avoid variable-length arrays, their support became optional after C99 --- drivers/solis.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/solis.c b/drivers/solis.c index a098f40453..76f4565770 100644 --- a/drivers/solis.c +++ b/drivers/solis.c @@ -682,7 +682,7 @@ static void get_base_info(void) { #else const char DaysOfWeek[7][4]={"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; #endif - unsigned char packet[packet_size], syncEOR; + unsigned char packet[PACKET_SIZE], syncEOR; int i1=0, i2=0, tam, i; time_t tmt;