Skip to content

Commit a3e0664

Browse files
committed
Reconcile int-ish data types with formatting strings [#823]
New checks in clang-18/19 brought new findings, some of them reasonable :) * "%x" style formatting requires an (unsigned int) variable * numeric literals and macros are (int) by default * results of math with (uint16_t), done in some cases, are upscaled into (int) by default * char, unsigned or not, seems to be also upscaled into (int) Overall, had to change formatting strings in some cases, variable types in others (e.g. flags or notification types do not make sense as signed) and added casting in a few places that remained. Signed-off-by: Jim Klimov <[email protected]>
1 parent fe14c1f commit a3e0664

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+249
-209
lines changed

NEWS.adoc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,16 @@ relocated into new `shutdown.default` INSTCMD definitions. [#2670]
457457
`gcc-13`+ whose static analyzers on NUT CI farm complained about some
458458
imperfections after adding newer OS revisions to the population of
459459
build agents. [#2585, #2588]
460+
* New checks in `clang-19` brought new findings about mismatched formatting
461+
strings and `int`-ish parameters of respective methods.
462+
Overall, had to change formatting strings in some cases, variable types
463+
in others (e.g. flags or notification types do not make sense as signed)
464+
and added casting in a few places that remained, because:
465+
- `%x` style formatting requires an `unsigned int` variable
466+
- numeric literals and macros are `int` by default
467+
- results of math with unsigned types like `uint16_t`, done in some
468+
cases, are up-scaled into `int` by default
469+
- `char`'s, `unsigned` or not, seem to be also up-scaled into `int`
460470
461471
- updated `docs/nut-names.txt` with items defined by 42ITy NUT fork. [#2339]
462472

clients/upscmd.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ static void usage(const char *prog)
6161
printf(" -p <password> set password for command authentication\n");
6262
printf(" -w wait for the completion of command by the driver\n");
6363
printf(" and return its actual result from the device\n");
64-
printf(" -t <timeout> set a timeout when using -w (in seconds, default: %u)\n", DEFAULT_TRACKING_TIMEOUT);
64+
printf(" -t <timeout> set a timeout when using -w (in seconds, default: %d)\n", DEFAULT_TRACKING_TIMEOUT);
6565
printf("\n");
6666
printf(" <ups> UPS identifier - <upsname>[@<hostname>[:<port>]]\n");
6767
printf(" <command> Valid instant command - test.panel.start, etc.\n");

clients/upslog.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ static void do_etime(const char *arg)
351351
NUT_UNUSED_VARIABLE(arg);
352352

353353
time(&tod);
354-
snprintfcat(logbuffer, sizeof(logbuffer), "%ld", (unsigned long) tod);
354+
snprintfcat(logbuffer, sizeof(logbuffer), "%lu", (unsigned long) tod);
355355
}
356356

357357
static void print_literal(const char *arg)

clients/upsmon.c

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ static unsigned __stdcall async_notify(LPVOID param)
290290
}
291291
#endif
292292

293-
static void notify(const char *notice, int flags, const char *ntype,
293+
static void notify(const char *notice, unsigned int flags, const char *ntype,
294294
const char *upsname)
295295
{
296296
#ifndef WIN32
@@ -380,7 +380,7 @@ static void notify(const char *notice, int flags, const char *ntype,
380380
#endif
381381
}
382382

383-
static void do_notify(const utype_t *ups, int ntype, const char *extra)
383+
static void do_notify(const utype_t *ups, unsigned int ntype, const char *extra)
384384
{
385385
int i;
386386
char msg[SMALLBUF], *upsname = NULL;
@@ -394,7 +394,8 @@ static void do_notify(const utype_t *ups, int ntype, const char *extra)
394394
const char *msgfmt = (notifylist[i].msg ? notifylist[i].msg : notifylist[i].stockmsg);
395395
char *s = strstr(msgfmt, "%s");
396396

397-
upsdebugx(2, "%s: ntype 0x%04x (%s)", __func__, ntype,
397+
upsdebugx(2, "%s: ntype 0x%04x (%s)",
398+
__func__, ntype,
398399
notifylist[i].name);
399400
#ifdef HAVE_PRAGMAS_FOR_GCC_DIAGNOSTIC_IGNORED_FORMAT_NONLITERAL
400401
#pragma GCC diagnostic push
@@ -1371,8 +1372,8 @@ static int is_ups_critical(utype_t *ups)
13711372
"%s: seems that UPS [%s] is OB+LB now, "
13721373
"but OBLBDURATION=%d - not declaring "
13731374
"a critical state just now, but will "
1374-
"bump the polling rate to %d (from "
1375-
"POLLFREQALERT=%d) and wait to see "
1375+
"bump the polling rate to %u (from "
1376+
"POLLFREQALERT=%u) and wait to see "
13761377
"if this situation dissipates soon",
13771378
__func__, ups->upsname,
13781379
oblbdurationtime,
@@ -1604,7 +1605,7 @@ static void redefine_ups(utype_t *ups, unsigned int pv, const char *un,
16041605
* from seeing it again? This is a somewhat rare case
16051606
* of self-inflicted pain, if it does even happen :)
16061607
*/
1607-
upslogx(LOG_INFO, "UPS [%s]: redefined power value to %d",
1608+
upslogx(LOG_INFO, "UPS [%s]: redefined power value to %u",
16081609
ups->sys, pv);
16091610
ups->pv = pv;
16101611
}
@@ -1818,7 +1819,7 @@ static void addups(int reloading, const char *sys, const char *pvs,
18181819
/* Negative debug value may be set by help() to be really quiet */
18191820
if (nut_debug_level > -1) {
18201821
if (tmp->pv)
1821-
upslogx(LOG_INFO, "UPS: %s (%s) (power value %d)", tmp->sys,
1822+
upslogx(LOG_INFO, "UPS: %s (%s) (power value %u)", tmp->sys,
18221823
flag_isset(tmp->status, ST_PRIMARY) ? "primary" : "secondary",
18231824
tmp->pv);
18241825
else
@@ -2835,7 +2836,7 @@ static void pollups(utype_t *ups)
28352836
upslogx(LOG_ERR, "Poll UPS [%s] failure state code "
28362837
"changed from %d to %d; "
28372838
"report below will only be repeated to syslog "
2838-
"every %d polling loop cycles (%d sec):",
2839+
"every %d polling loop cycles (%u sec):",
28392840
ups->sys, ups->pollfail_log_throttle_state,
28402841
upserror, pollfail_log_throttle_max,
28412842
pollfail_log_throttle_max * pollfreq);
@@ -3202,8 +3203,8 @@ static void reload_conf(void)
32023203

32033204
/* see if the user just blew off a foot */
32043205
if (totalpv < minsupplies) {
3205-
upslogx(LOG_CRIT, "Fatal error: total power value (%d) less "
3206-
"than MINSUPPLIES (%d)", totalpv, minsupplies);
3206+
upslogx(LOG_CRIT, "Fatal error: total power value (%u) less "
3207+
"than MINSUPPLIES (%u)", totalpv, minsupplies);
32073208

32083209
fatalx(EXIT_FAILURE, "Impossible power configuration, unable to continue");
32093210
}
@@ -3577,8 +3578,8 @@ int main(int argc, char *argv[])
35773578
if (totalpv < minsupplies) {
35783579
printf("\nFatal error: insufficient power configured!\n\n");
35793580

3580-
printf("Sum of power values........: %d\n", totalpv);
3581-
printf("Minimum value (MINSUPPLIES): %d\n", minsupplies);
3581+
printf("Sum of power values........: %u\n", totalpv);
3582+
printf("Minimum value (MINSUPPLIES): %u\n", minsupplies);
35823583

35833584
printf("\nEdit your upsmon.conf and change the values.\n");
35843585
exit(EXIT_FAILURE);

clients/upsmon.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ typedef struct {
149149
* To move or not to move?..
150150
*/
151151
static struct {
152-
int type;
152+
unsigned int type;
153153
const char *name;
154154
char *msg; /* NULL until overridden */
155155
const char *stockmsg;

clients/upsrw.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ static void usage(const char *prog)
6262
printf(" -p <password> set password for command authentication\n");
6363
printf(" -w wait for the completion of setting by the driver\n");
6464
printf(" and return its actual result from the device\n");
65-
printf(" -t <timeout> set a timeout when using -w (in seconds, default: %u)\n", DEFAULT_TRACKING_TIMEOUT);
65+
printf(" -t <timeout> set a timeout when using -w (in seconds, default: %d)\n", DEFAULT_TRACKING_TIMEOUT);
6666
printf("\n");
6767
printf(" <ups> UPS identifier - <upsname>[@<hostname>[:<port>]]\n");
6868
printf("\n");

common/common.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4298,7 +4298,7 @@ int match_regex_hex(const regex_t *preg, const int n)
42984298
{
42994299
char buf[10];
43004300

4301-
snprintf(buf, sizeof(buf), "%04x", n);
4301+
snprintf(buf, sizeof(buf), "%04x", (unsigned int)n);
43024302

43034303
return match_regex(preg, buf);
43044304
}

common/parseconf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ static void addchar(PCONF_CTX_t *ctx)
184184
/* CVE-2012-2944: only allow the subset of ASCII charset from Space to ~ */
185185
if ((ctx->ch < 0x20) || (ctx->ch > 0x7f)) {
186186
fprintf(stderr, "addchar: discarding invalid character (0x%02x)!\n",
187-
ctx->ch);
187+
(unsigned int)ctx->ch);
188188
return;
189189
}
190190

drivers/adelsystem_cbi.c

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -592,12 +592,12 @@ void reginit(void)
592592
*/
593593
default:
594594
upslogx(LOG_ERR,
595-
"Invalid register type %d for register %d",
595+
"Invalid register type %u for register %d",
596596
regs[i].type,
597597
regs[i].num
598598
);
599599
upsdebugx(3,
600-
"Invalid register type %d for register %d",
600+
"Invalid register type %u for register %d",
601601
regs[i].type,
602602
regs[i].num
603603
);
@@ -609,11 +609,11 @@ void reginit(void)
609609
#endif
610610
}
611611
upsdebugx(3,
612-
"reginit: num:%d, type: %d saddr: %d, xaddr: 0x%x",
612+
"reginit: num:%d, type: %u saddr: %d, xaddr: 0x%x",
613613
regs[i].num,
614614
regs[i].type,
615615
regs[i].saddr,
616-
regs[i].xaddr
616+
(unsigned int)(regs[i].xaddr)
617617
);
618618
}
619619
}
@@ -627,9 +627,9 @@ int read_all_regs(modbus_t *mb, uint16_t *data)
627627
rval = modbus_read_registers(mb, regs[H_REG_STARTIDX].xaddr, MAX_H_REGS, data);
628628
if (rval == -1) {
629629
upslogx(LOG_ERR,
630-
"ERROR:(%s) modbus_read: addr:0x%x, length:%8d, path:%s\n",
630+
"ERROR:(%s) modbus_read: addr:0x%x, length:%8d, path:%s",
631631
modbus_strerror(errno),
632-
regs[H_REG_STARTIDX].xaddr,
632+
(unsigned int)(regs[H_REG_STARTIDX].xaddr),
633633
MAX_H_REGS,
634634
device_path
635635
);
@@ -692,7 +692,7 @@ int register_read(modbus_t *mb, int addr, regtype_t type, void *data)
692692
* memory corruptions and buggy inputs below...
693693
*/
694694
default:
695-
upsdebugx(2,"ERROR: register_read: invalid register type %d\n", type);
695+
upsdebugx(2, "ERROR: register_read: invalid register type %u", type);
696696
break;
697697
#ifdef __clang__
698698
# pragma clang diagnostic pop
@@ -703,9 +703,9 @@ int register_read(modbus_t *mb, int addr, regtype_t type, void *data)
703703
}
704704
if (rval == -1) {
705705
upslogx(LOG_ERR,
706-
"ERROR:(%s) modbus_read: addr:0x%x, type:%8s, path:%s\n",
706+
"ERROR:(%s) modbus_read: addr:0x%x, type:%8s, path:%s",
707707
modbus_strerror(errno),
708-
addr,
708+
(unsigned int)addr,
709709
(type == COIL) ? "COIL" :
710710
(type == INPUT_B) ? "INPUT_B" :
711711
(type == INPUT_R) ? "INPUT_R" : "HOLDING",
@@ -718,7 +718,8 @@ int register_read(modbus_t *mb, int addr, regtype_t type, void *data)
718718
modbus_reconnect();
719719
}
720720
}
721-
upsdebugx(3, "register addr: 0x%x, register type: %d read: %u",addr, type, *(unsigned int *)data);
721+
upsdebugx(3, "register addr: 0x%x, register type: %u read: %u",
722+
(unsigned int)addr, type, *(unsigned int *)data);
722723
return rval;
723724
}
724725

@@ -755,14 +756,14 @@ int register_write(modbus_t *mb, int addr, regtype_t type, void *data)
755756
#if (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_PUSH_POP) && (defined HAVE_PRAGMA_GCC_DIAGNOSTIC_IGNORED_COVERED_SWITCH_DEFAULT)
756757
# pragma GCC diagnostic pop
757758
#endif
758-
upsdebugx(2,"ERROR: register_write: invalid register type %d\n", type);
759+
upsdebugx(2, "ERROR: register_write: invalid register type %u", type);
759760
break;
760761
}
761762
if (rval == -1) {
762763
upslogx(LOG_ERR,
763-
"ERROR:(%s) modbus_write: addr:0x%x, type:%8s, path:%s\n",
764+
"ERROR:(%s) modbus_write: addr:0x%x, type:%8s, path:%s",
764765
modbus_strerror(errno),
765-
addr,
766+
(unsigned int)addr,
766767
(type == COIL) ? "COIL" :
767768
(type == INPUT_B) ? "INPUT_B" :
768769
(type == INPUT_R) ? "INPUT_R" : "HOLDING",
@@ -775,7 +776,8 @@ int register_write(modbus_t *mb, int addr, regtype_t type, void *data)
775776
modbus_reconnect();
776777
}
777778
}
778-
upsdebugx(3, "register addr: 0x%x, register type: %d read: %u",addr, type, *(unsigned int *)data);
779+
upsdebugx(3, "register addr: 0x%x, register type: %u read: %u",
780+
(unsigned int)addr, type, *(unsigned int *)data);
779781
return rval;
780782
}
781783

@@ -815,16 +817,17 @@ int upscmd(const char *cmd, const char *arg)
815817
rval = register_write(mbctx, regs[FSD].xaddr, regs[FSD].type, &data);
816818
if (rval == -1) {
817819
upslogx(2,
818-
"ERROR:(%s) modbus_write_register: addr:0x%08x, regtype: %d, path:%s\n",
820+
"ERROR:(%s) modbus_write_register: addr:0x%08x, regtype: %u, path:%s",
819821
modbus_strerror(errno),
820-
regs[FSD].xaddr,
822+
(unsigned int)(regs[FSD].xaddr),
821823
regs[FSD].type,
822824
device_path
823825
);
824826
upslogx(LOG_NOTICE, "load.off: failed (communication error) [%s] [%s]", cmd, arg);
825827
rval = STAT_INSTCMD_FAILED;
826828
} else {
827-
upsdebugx(2, "load.off: addr: 0x%x, data: %d", regs[FSD].xaddr, data);
829+
upsdebugx(2, "load.off: addr: 0x%x, data: %d",
830+
(unsigned int)(regs[FSD].xaddr), data);
828831
rval = STAT_INSTCMD_HANDLED;
829832
}
830833
} else if (!strcasecmp(cmd, "shutdown.stayoff")) {
@@ -843,7 +846,7 @@ int upscmd(const char *cmd, const char *arg)
843846

844847
/* wait for an increasing time interval before sending shutdown command */
845848
while ((etime = time_elapsed(&start)) < ( FSD_REPEAT_INTRV / cnt));
846-
upsdebugx(2, "ERROR: load.off failed, wait for %lims, retries left: %d\n", etime, cnt - 1);
849+
upsdebugx(2, "ERROR: load.off failed, wait for %lims, retries left: %d", etime, cnt - 1);
847850
cnt--;
848851
}
849852
switch (rval) {
@@ -959,13 +962,13 @@ int get_dev_state(devreg_t regindx, devstate_t **dvstat)
959962
char *reg_val_s;
960963

961964
state->reg.val.ui16 = reg_val;
962-
n = snprintf(NULL, 0, "%d", reg_val);
965+
n = snprintf(NULL, 0, "%u", reg_val);
963966
if (ptr != NULL) {
964967
free(ptr);
965968
}
966969
reg_val_s = (char *)xmalloc(sizeof(char) * (n + 1));
967970
ptr = reg_val_s;
968-
sprintf(reg_val_s, "%d", reg_val);
971+
sprintf(reg_val_s, "%u", reg_val);
969972
state->reg.strval = reg_val_s;
970973
} else {
971974
state->reg.val.ui16 = 0;
@@ -1203,13 +1206,13 @@ int get_dev_state(devreg_t regindx, devstate_t **dvstat)
12031206
{ /* scoping */
12041207
char *reg_val_s;
12051208
state->reg.val.ui16 = reg_val;
1206-
n = snprintf(NULL, 0, "%d", reg_val);
1209+
n = snprintf(NULL, 0, "%u", reg_val);
12071210
if (ptr != NULL) {
12081211
free(ptr);
12091212
}
12101213
reg_val_s = (char *)xmalloc(sizeof(char) * (n + 1));
12111214
ptr = reg_val_s;
1212-
sprintf(reg_val_s, "%d", reg_val);
1215+
sprintf(reg_val_s, "%u", reg_val);
12131216
state->reg.strval = reg_val_s;
12141217
}
12151218
break;
@@ -1268,31 +1271,31 @@ void get_config_vars(void)
12681271
if (testvar("mod_resp_to_s")) {
12691272
mod_resp_to_s = (uint32_t)strtol(getval("mod_resp_to_s"), NULL, 10);
12701273
}
1271-
upsdebugx(2, "mod_resp_to_s %d", mod_resp_to_s);
1274+
upsdebugx(2, "mod_resp_to_s %u", mod_resp_to_s);
12721275

12731276
/* check if response time out (us) is set and get the value */
12741277
if (testvar("mod_resp_to_us")) {
12751278
mod_resp_to_us = (uint32_t) strtol(getval("mod_resp_to_us"), NULL, 10);
12761279
if (mod_resp_to_us > 999999) {
1277-
fatalx(EXIT_FAILURE, "get_config_vars: Invalid mod_resp_to_us %d", mod_resp_to_us);
1280+
fatalx(EXIT_FAILURE, "get_config_vars: Invalid mod_resp_to_us %u", mod_resp_to_us);
12781281
}
12791282
}
1280-
upsdebugx(2, "mod_resp_to_us %d", mod_resp_to_us);
1283+
upsdebugx(2, "mod_resp_to_us %u", mod_resp_to_us);
12811284

12821285
/* check if byte time out (s) is set and get the value */
12831286
if (testvar("mod_byte_to_s")) {
12841287
mod_byte_to_s = (uint32_t)strtol(getval("mod_byte_to_s"), NULL, 10);
12851288
}
1286-
upsdebugx(2, "mod_byte_to_s %d", mod_byte_to_s);
1289+
upsdebugx(2, "mod_byte_to_s %u", mod_byte_to_s);
12871290

12881291
/* check if byte time out (us) is set and get the value */
12891292
if (testvar("mod_byte_to_us")) {
12901293
mod_byte_to_us = (uint32_t) strtol(getval("mod_byte_to_us"), NULL, 10);
12911294
if (mod_byte_to_us > 999999) {
1292-
fatalx(EXIT_FAILURE, "get_config_vars: Invalid mod_byte_to_us %d", mod_byte_to_us);
1295+
fatalx(EXIT_FAILURE, "get_config_vars: Invalid mod_byte_to_us %u", mod_byte_to_us);
12931296
}
12941297
}
1295-
upsdebugx(2, "mod_byte_to_us %d", mod_byte_to_us);
1298+
upsdebugx(2, "mod_byte_to_us %u", mod_byte_to_us);
12961299
}
12971300

12981301
/* create a new modbus context based on connection type (serial or TCP) */
@@ -1303,21 +1306,21 @@ modbus_t *modbus_new(const char *port)
13031306
if (strstr(port, "/dev/tty") != NULL) {
13041307
mb = modbus_new_rtu(port, ser_baud_rate, ser_parity, ser_data_bit, ser_stop_bit);
13051308
if (mb == NULL) {
1306-
upslogx(LOG_ERR, "modbus_new_rtu: Unable to open serial port context\n");
1309+
upslogx(LOG_ERR, "modbus_new_rtu: Unable to open serial port context");
13071310
}
13081311
} else if ((sp = strchr(port, ':')) != NULL) {
13091312
char *tcp_port = xmalloc(sizeof(sp));
13101313
strcpy(tcp_port, sp + 1);
13111314
*sp = '\0';
13121315
mb = modbus_new_tcp(port, (int)strtoul(tcp_port, NULL, 10));
13131316
if (mb == NULL) {
1314-
upslogx(LOG_ERR, "modbus_new_tcp: Unable to connect to %s\n", port);
1317+
upslogx(LOG_ERR, "modbus_new_tcp: Unable to connect to %s", port);
13151318
}
13161319
free(tcp_port);
13171320
} else {
13181321
mb = modbus_new_tcp(port, 502);
13191322
if (mb == NULL) {
1320-
upslogx(LOG_ERR, "modbus_new_tcp: Unable to connect to %s\n", port);
1323+
upslogx(LOG_ERR, "modbus_new_tcp: Unable to connect to %s", port);
13211324
}
13221325
}
13231326
return mb;

0 commit comments

Comments
 (0)