Skip to content

Commit

Permalink
drivers/nutdrv_qx_bestups.c: bestups_batt_packs(): range-check and pr…
Browse files Browse the repository at this point in the history
…operly cast the value, and harden with snprintf_dynamic() [networkupstools#2450]

Signed-off-by: Jim Klimov <[email protected]>
  • Loading branch information
jimklimov committed Jun 2, 2024
1 parent 6946774 commit 84404b9
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions drivers/nutdrv_qx_bestups.c
Original file line number Diff line number Diff line change
Expand Up @@ -539,25 +539,20 @@ static int bestups_batt_runtime(item_t *item, char *value, const size_t valuelen
static int bestups_batt_packs(item_t *item, char *value, const size_t valuelen)
{
item_t *unskip;
long l;

if (strspn(item->value, "0123456789 ") != strlen(item->value)) {
upsdebugx(2, "%s: non numerical value [%s: %s]", __func__, item->info_type, item->value);
return -1;
}

#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_FORMAT_NONLITERAL
#pragma GCC diagnostic push
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_FORMAT_NONLITERAL
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
#endif
#ifdef HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_FORMAT_SECURITY
#pragma GCC diagnostic ignored "-Wformat-security"
#endif
snprintf(value, valuelen, item->dfl, strtol(item->value, NULL, 10));
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_FORMAT_NONLITERAL
#pragma GCC diagnostic pop
#endif
l = strtol(item->value, NULL, 10);
if (l < 0 || l > INT_MAX) {
upsdebugx(2, "%s: value out of range [%s: %s]", __func__, item->info_type, item->value);
return -1;
}

snprintf_dynamic(value, valuelen, item->dfl, "%d", (int)l);

/* Unskip battery.packs setvar */
unskip = find_nut_info("battery.packs", QX_FLAG_SETVAR, 0);
Expand Down

0 comments on commit 84404b9

Please sign in to comment.