fix printf()/scanf() format signedness

Fix mismatches between the format and sign of variables passed to
printf() or scanf(), which were found in a Frama-C analysis and gcc
using the -Wformat-signedness option.
This commit is contained in:
Miroslav Lichvar 2018-06-20 16:28:10 +02:00
parent 9b34556952
commit 9bc774d6af
10 changed files with 30 additions and 29 deletions

View file

@ -1638,17 +1638,17 @@ print_seconds(unsigned long s)
if (s == (uint32_t)-1) {
printf(" -");
} else if (s < 1200) {
printf("%4ld", s);
printf("%4lu", s);
} else if (s < 36000) {
printf("%3ldm", s / 60);
printf("%3lum", s / 60);
} else if (s < 345600) {
printf("%3ldh", s / 3600);
printf("%3luh", s / 3600);
} else {
d = s / 86400;
if (d > 999) {
printf("%3ldy", d / 365);
printf("%3luy", d / 365);
} else {
printf("%3ldd", d);
printf("%3lud", d);
}
}
}
@ -2890,7 +2890,7 @@ process_cmd_keygen(char *line)
snprintf(hash_name, sizeof (hash_name), "MD5");
#endif
if (sscanf(line, "%u %16s %d", &id, hash_name, &bits))
if (sscanf(line, "%u %16s %u", &id, hash_name, &bits))
;
length = CLAMP(10, (bits + 7) / 8, sizeof (key));

2
keys.c
View file

@ -214,7 +214,7 @@ KEY_Reload(void)
continue;
if (!CPS_ParseKey(line, &key_id, &hashname, &keyval)) {
LOG(LOGS_WARN, "Could not parse key at line %d in file %s", line_number, key_file);
LOG(LOGS_WARN, "Could not parse key at line %u in file %s", line_number, key_file);
continue;
}

View file

@ -2043,7 +2043,7 @@ NCR_ProcessRxKnown(NCR_Instance inst, NTP_Local_Address *local_addr,
/* It's not a reply to our request, don't return success */
return 0;
} else {
DEBUG_LOG("NTP packet discarded pkt_mode=%d our_mode=%d", pkt_mode, inst->mode);
DEBUG_LOG("NTP packet discarded pkt_mode=%d our_mode=%u", pkt_mode, inst->mode);
return 0;
}
}
@ -2102,7 +2102,7 @@ NCR_ProcessRxUnknown(NTP_Remote_Address *remote_addr, NTP_Local_Address *local_a
/* Fall through */
default:
/* Discard */
DEBUG_LOG("NTP packet discarded pkt_mode=%d", pkt_mode);
DEBUG_LOG("NTP packet discarded pkt_mode=%u", pkt_mode);
return;
}
@ -2128,7 +2128,7 @@ NCR_ProcessRxUnknown(NTP_Remote_Address *remote_addr, NTP_Local_Address *local_a
break;
default:
/* Discard packets in other modes */
DEBUG_LOG("NTP packet discarded auth_mode=%d", auth_mode);
DEBUG_LOG("NTP packet discarded auth_mode=%u", auth_mode);
return;
}
}

View file

@ -684,7 +684,7 @@ process_message(struct msghdr *hdr, int length, int sock_fd)
return;
#endif
DEBUG_LOG("Received %d bytes from %s:%d to %s fd=%d if=%d tss=%d delay=%.9f",
DEBUG_LOG("Received %d bytes from %s:%d to %s fd=%d if=%d tss=%u delay=%.9f",
length, UTI_IPToString(&remote_addr.ip_addr), remote_addr.port,
UTI_IPToString(&local_addr.ip_addr), local_addr.sock_fd, local_addr.if_index,
local_ts.source, UTI_DiffTimespecsToDouble(&sched_ts, &local_ts.ts));

View file

@ -321,7 +321,7 @@ check_timestamping_option(int option)
return 0;
if (setsockopt(sock_fd, SOL_SOCKET, SO_TIMESTAMPING, &option, sizeof (option)) < 0) {
DEBUG_LOG("Could not enable timestamping option %x", option);
DEBUG_LOG("Could not enable timestamping option %x", (unsigned int)option);
close(sock_fd);
return 0;
}
@ -790,7 +790,7 @@ NIO_Linux_ProcessMessage(NTP_Remote_Address *remote_addr, NTP_Local_Address *loc
l2_length = length;
length = extract_udp_data(hdr->msg_iov[0].iov_base, remote_addr, length);
DEBUG_LOG("Received %d (%d) bytes from error queue for %s:%d fd=%d if=%d tss=%d",
DEBUG_LOG("Received %d (%d) bytes from error queue for %s:%d fd=%d if=%d tss=%u",
l2_length, length, UTI_IPToString(&remote_addr->ip_addr), remote_addr->port,
local_addr->sock_fd, local_addr->if_index, local_ts->source);

View file

@ -82,7 +82,7 @@ static void read_sample(int sockfd, int event, void *anything)
if (sample.magic != SOCK_MAGIC) {
LOG(LOGS_WARN, "Unexpected magic number in SOCK sample : %x != %x",
sample.magic, SOCK_MAGIC);
(unsigned int)sample.magic, (unsigned int)SOCK_MAGIC);
return;
}

View file

@ -778,8 +778,8 @@ SRC_SelectSource(SRC_Instance updated_inst)
}
DEBUG_LOG("badstat=%d sel=%d badstat_reach=%x sel_reach=%x max_reach_ago=%f",
n_badstats_sources, n_sel_sources, max_badstat_reach,
max_sel_reach, max_reach_sample_ago);
n_badstats_sources, n_sel_sources, (unsigned int)max_badstat_reach,
(unsigned int)max_sel_reach, max_reach_sample_ago);
/* Wait for the next call if we have no source selected and there is
a source with bad stats (has less than 3 samples) with reachability
@ -1015,7 +1015,7 @@ SRC_SelectSource(SRC_Instance updated_inst)
sources[i]->sel_score = 1.0 / distance;
}
DEBUG_LOG("select score=%f refid=%"PRIx32" match_refid=%"PRIx32" status=%d dist=%f",
DEBUG_LOG("select score=%f refid=%"PRIx32" match_refid=%"PRIx32" status=%u dist=%f",
sources[i]->sel_score, sources[i]->ref_id,
updated_inst ? updated_inst->ref_id : 0,
sources[i]->status, distance);

View file

@ -65,7 +65,7 @@ test_unit(void)
}
}
DEBUG_LOG("records %d", ARR_GetSize(records));
DEBUG_LOG("records %u", ARR_GetSize(records));
TEST_CHECK(ARR_GetSize(records) == 64);
for (i = j = 0; i < 10000; i++) {
@ -76,7 +76,7 @@ test_unit(void)
j++;
}
DEBUG_LOG("requests %u responses %u", i, j);
DEBUG_LOG("requests %d responses %d", i, j);
TEST_CHECK(j * 4 < i && j * 6 > i);
CLG_Finalise();

View file

@ -82,7 +82,7 @@ test_unit(void)
double passed_lo = DBL_MAX, passed_hi = DBL_MIN;
SRC_SelectSource(srcs[k]);
DEBUG_LOG("source %d status %d", k, sources[k]->status);
DEBUG_LOG("source %d status %u", k, sources[k]->status);
for (l = 0; l <= j; l++) {
TEST_CHECK(sources[l]->status > SRC_OK && sources[l]->status <= SRC_SELECTED);

19
util.c
View file

@ -298,16 +298,17 @@ UTI_IPToString(IPAddr *addr)
b = (ip>>16) & 0xff;
c = (ip>> 8) & 0xff;
d = (ip>> 0) & 0xff;
snprintf(result, BUFFER_LENGTH, "%ld.%ld.%ld.%ld", a, b, c, d);
snprintf(result, BUFFER_LENGTH, "%lu.%lu.%lu.%lu", a, b, c, d);
break;
case IPADDR_INET6:
ip6 = addr->addr.in6;
#ifdef FEAT_IPV6
inet_ntop(AF_INET6, ip6, result, BUFFER_LENGTH);
#else
snprintf(result, BUFFER_LENGTH, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
ip6[0], ip6[1], ip6[2], ip6[3], ip6[4], ip6[5], ip6[6], ip6[7],
ip6[8], ip6[9], ip6[10], ip6[11], ip6[12], ip6[13], ip6[14], ip6[15]);
assert(BUFFER_LENGTH >= 40);
for (a = 0; a < 8; a++)
snprintf(result + a * 5, 40 - a * 5, "%04x:",
(unsigned int)(ip6[2 * a] << 8 | ip6[2 * a + 1]));
#endif
break;
default:
@ -1160,12 +1161,12 @@ UTI_CheckDirPermissions(const char *path, mode_t perm, uid_t uid, gid_t gid)
}
if (buf.st_uid != uid) {
LOG(LOGS_ERR, "Wrong owner of %s (%s != %d)", path, "UID", uid);
LOG(LOGS_ERR, "Wrong owner of %s (%s != %u)", path, "UID", uid);
return 0;
}
if (buf.st_gid != gid) {
LOG(LOGS_ERR, "Wrong owner of %s (%s != %d)", path, "GID", gid);
LOG(LOGS_ERR, "Wrong owner of %s (%s != %u)", path, "GID", gid);
return 0;
}
@ -1183,13 +1184,13 @@ UTI_DropRoot(uid_t uid, gid_t gid)
/* Set effective, saved and real group ID */
if (setgid(gid))
LOG_FATAL("setgid(%d) failed : %s", gid, strerror(errno));
LOG_FATAL("setgid(%u) failed : %s", gid, strerror(errno));
/* Set effective, saved and real user ID */
if (setuid(uid))
LOG_FATAL("setuid(%d) failed : %s", uid, strerror(errno));
LOG_FATAL("setuid(%u) failed : %s", uid, strerror(errno));
DEBUG_LOG("Dropped root privileges: UID %d GID %d", uid, gid);
DEBUG_LOG("Dropped root privileges: UID %u GID %u", uid, gid);
}
/* ================================================== */