From 449dc16d47a2d2677b43aa3a987642ce50a4d41c Mon Sep 17 00:00:00 2001 From: Poul-Henning Kamp Date: Fri, 16 Jan 2015 21:21:15 +0000 Subject: [PATCH] A bit of signed/unsigned cleanup work. --- main_sim_client.c | 2 +- ntimed.h | 2 +- ntp.h | 2 +- ntp_packet.c | 10 +++++--- ntp_peer.c | 4 +-- ntp_tools.c | 62 +++++++++++++++++++++++------------------------ param.c | 3 ++- time_stuff.c | 6 +++-- udp.c | 2 +- udp.h | 2 +- 10 files changed, 49 insertions(+), 46 deletions(-) diff --git a/main_sim_client.c b/main_sim_client.c index dde8ddf..4da8972 100644 --- a/main_sim_client.c +++ b/main_sim_client.c @@ -121,7 +121,7 @@ simfile_readline(struct ocx *ocx, struct todolist *tdl, void *priv) while (1) { if (fgets(buf, sizeof buf, sf->input) == NULL) { - Debug(ocx, "EOF on -s file (%s)", sf->filename); + Debug(ocx, "EOF on -s file (%s)\n", sf->filename); exit(0); } p = strchr(buf, '\r'); diff --git a/ntimed.h b/ntimed.h index cf2eba3..491491a 100644 --- a/ntimed.h +++ b/ntimed.h @@ -132,7 +132,7 @@ struct timestamp *TS_Nanosec(struct timestamp *storage, struct timestamp *TS_Double(struct timestamp *storage, double); double TS_Diff(const struct timestamp *t1, const struct timestamp *t2); int TS_SleepUntil(const struct timestamp *); -void TS_Format(char *buf, ssize_t len, const struct timestamp *ts); +void TS_Format(char *buf, size_t len, const struct timestamp *ts); void TS_RunTest(struct ocx *ocx); diff --git a/ntp.h b/ntp.h index a253577..6821ea6 100644 --- a/ntp.h +++ b/ntp.h @@ -78,7 +78,7 @@ struct ntp_packet { struct ntp_packet *NTP_Packet_Unpack(struct ntp_packet *dst, void *ptr, ssize_t len); -ssize_t NTP_Packet_Pack(void *ptr, ssize_t len, struct ntp_packet *); +size_t NTP_Packet_Pack(void *ptr, ssize_t len, struct ntp_packet *); /* ntp_tools.c -- Handy tools *****************************************/ diff --git a/ntp_packet.c b/ntp_packet.c index 32976c3..7d434f3 100644 --- a/ntp_packet.c +++ b/ntp_packet.c @@ -136,7 +136,7 @@ ts_2ntp32(uint8_t *dst, const struct timestamp *ts) CHECK_OBJ_NOTNULL(ts, TIMESTAMP_MAGIC); assert(ts->sec < 65536); - Be16enc(dst, ts->sec); + Be16enc(dst, (uint16_t)ts->sec); Be16enc(dst + 2, ts->frac >> 48ULL); } @@ -149,7 +149,7 @@ ts_2ntp64(uint8_t *dst, const struct timestamp *ts) Be32enc(dst + 4, ts->frac >> 32ULL); } -ssize_t +size_t NTP_Packet_Pack(void *ptr, ssize_t len, struct ntp_packet *np) { uint8_t *pbuf = ptr; @@ -160,8 +160,10 @@ NTP_Packet_Pack(void *ptr, ssize_t len, struct ntp_packet *np) assert(np->ntp_version < 8); assert(np->ntp_stratum < 15); - pbuf[0] = (uint8_t)np->ntp_leap << 6; - pbuf[0] |= np->ntp_version << 3; + pbuf[0] = (uint8_t)np->ntp_leap; + pbuf[0] <<= 3; + pbuf[0] |= np->ntp_version; + pbuf[0] <<= 3; pbuf[0] |= (uint8_t)np->ntp_mode; pbuf[1] = np->ntp_stratum; pbuf[2] = np->ntp_poll; diff --git a/ntp_peer.c b/ntp_peer.c index e915a31..8a6383d 100644 --- a/ntp_peer.c +++ b/ntp_peer.c @@ -106,7 +106,7 @@ NTP_Peer_Poll(struct ocx *ocx, const struct udp_socket *usc, const struct ntp_peer *np, double tmo) { char buf[100]; - ssize_t len; + size_t len; struct sockaddr_storage rss; socklen_t rssl; ssize_t l; @@ -121,7 +121,7 @@ NTP_Peer_Poll(struct ocx *ocx, const struct udp_socket *usc, len = NTP_Packet_Pack(buf, sizeof buf, np->tx_pkt); l = Udp_Send(ocx, usc, np->sa, np->sa_len, buf, len); - if (l != len) { + if (l != (ssize_t)len) { Debug(ocx, "Tx peer %s %s got %zd (%s)\n", np->hostname, np->ip, l, strerror(errno)); return (0); diff --git a/ntp_tools.c b/ntp_tools.c index c71d2be..be68b3d 100644 --- a/ntp_tools.c +++ b/ntp_tools.c @@ -32,6 +32,7 @@ #include #include #include +#include #include "ntimed.h" #include "ntp.h" @@ -70,6 +71,17 @@ NTP_Tool_Client_Req(struct ntp_packet *np) * XXX: Nanosecond precision is enough for everybody. */ +static void __printflike(3, 4) +bxprintf(char **bp, const char *e, const char *fmt, ...) +{ + va_list ap; + + assert(*bp < e); + va_start(ap, fmt); + *bp += vsnprintf(*bp, (unsigned)(e - *bp), fmt, ap); + va_end(ap); +} + void NTP_Tool_Format(char *p, ssize_t len, const struct ntp_packet *pkt) { @@ -82,58 +94,44 @@ NTP_Tool_Format(char *p, ssize_t len, const struct ntp_packet *pkt) e = p + len; - p += snprintf(p, e - p, "[%d", pkt->ntp_leap); - assert(p < e); + bxprintf(&p, e, "[%d", pkt->ntp_leap); + bxprintf(&p, e, " %u", pkt->ntp_version); - p += snprintf(p, e - p, " %u", pkt->ntp_version); - assert(p < e); + bxprintf(&p, e, " %d", pkt->ntp_mode); - p += snprintf(p, e - p, " %d", pkt->ntp_mode); - assert(p < e); + bxprintf(&p, e, " %3u", pkt->ntp_stratum); - p += snprintf(p, e - p, " %3u", pkt->ntp_stratum); - assert(p < e); + bxprintf(&p, e, " %3u", pkt->ntp_poll); - p += snprintf(p, e - p, " %3u", pkt->ntp_poll); - assert(p < e); - - p += snprintf(p, e - p, " %4d", pkt->ntp_precision); - assert(p < e); + bxprintf(&p, e, " %4d", pkt->ntp_precision); TS_Format(buf, sizeof buf, &pkt->ntp_delay); - p += snprintf(p, e - p, " %s", buf); assert(p < e); - assert(p < e); + bxprintf(&p, e, " %s", buf); assert(p < e); TS_Format(buf, sizeof buf, &pkt->ntp_dispersion); - p += snprintf(p, e - p, " %s", buf); assert(p < e); - assert(p < e); + bxprintf(&p, e, " %s", buf); assert(p < e); - p += snprintf(p, e - p, " 0x%02x%02x%02x%02x", + bxprintf(&p, e, " 0x%02x%02x%02x%02x", pkt->ntp_refid[0], pkt->ntp_refid[1], pkt->ntp_refid[2], pkt->ntp_refid[3]); - assert(p < e); - p += snprintf(p, e - p, " %.9f", + bxprintf(&p, e, " %.9f", TS_Diff(&pkt->ntp_reference, &pkt->ntp_origin)); - assert(p < e); TS_Format(buf, sizeof buf, &pkt->ntp_origin); - p += snprintf(p, e - p, " %s", buf); assert(p < e); - assert(p < e); + bxprintf(&p, e, " %s", buf); assert(p < e); - p += snprintf(p, e - p, " %.9f", + bxprintf(&p, e, " %.9f", TS_Diff(&pkt->ntp_receive, &pkt->ntp_origin)); - assert(p < e); - p += snprintf(p, e - p, " %.9f", + bxprintf(&p, e, " %.9f", TS_Diff(&pkt->ntp_transmit, &pkt->ntp_receive)); - assert(p < e); if (pkt->ts_rx.sec && pkt->ts_rx.frac) { - p += snprintf(p, e - p, " %.9f]", + bxprintf(&p, e, " %.9f]", TS_Diff(&pkt->ts_rx, &pkt->ntp_transmit)); } else { - p += snprintf(p, e - p, " %.9f]", 0.0); + bxprintf(&p, e, " %.9f]", 0.0); } assert(p < e); } @@ -173,10 +171,10 @@ NTP_Tool_Scan(struct ntp_packet *pkt, const char *buf) INIT_OBJ(pkt, NTP_PACKET_MAGIC); pkt->ntp_leap = (enum ntp_leap)u_fields[0]; - pkt->ntp_version = u_fields[1]; + pkt->ntp_version = (uint8_t)u_fields[1]; pkt->ntp_mode = (enum ntp_mode)u_fields[2]; - pkt->ntp_stratum = u_fields[3]; - pkt->ntp_poll = u_fields[4]; + pkt->ntp_stratum = (uint8_t)u_fields[3]; + pkt->ntp_poll = (uint8_t)u_fields[4]; pkt->ntp_precision = (int8_t)floor(d_fields[0]); TS_Double(&pkt->ntp_delay, d_fields[1]); TS_Double(&pkt->ntp_dispersion, d_fields[2]); diff --git a/param.c b/param.c index 72e391e..62f0616 100644 --- a/param.c +++ b/param.c @@ -121,7 +121,8 @@ Param_Tweak(struct ocx *ocx, const char *arg) Fail(ocx, 0, "Stopping after parameter query.\n"); } - l = q - arg; + assert (q >= arg); + l = (unsigned)(q - arg); TAILQ_FOREACH(pt, ¶m_tbl, list) { if (strlen(pt->name) != l) diff --git a/time_stuff.c b/time_stuff.c index d003738..fe1376a 100644 --- a/time_stuff.c +++ b/time_stuff.c @@ -140,10 +140,11 @@ TS_SleepUntil(const struct timestamp *t) /**********************************************************************/ void -TS_Format(char *buf, ssize_t len, const struct timestamp *ts) +TS_Format(char *buf, size_t len, const struct timestamp *ts) { CHECK_OBJ_NOTNULL(ts, TIMESTAMP_MAGIC); uint64_t x, y; + int i; /* XXX: Nanosecond precision is enough for everybody. */ x = ts->sec; @@ -152,7 +153,8 @@ TS_Format(char *buf, ssize_t len, const struct timestamp *ts) y -= 1000000000ULL; x += 1; } - assert(snprintf(buf, len, "%jd.%09jd", (intmax_t)x, (intmax_t)y) < len); + i = snprintf(buf, len, "%jd.%09jd", (intmax_t)x, (intmax_t)y); + assert(i < (int)len); } /********************************************************************** diff --git a/udp.c b/udp.c index 947b8d6..8c876e7 100644 --- a/udp.c +++ b/udp.c @@ -182,7 +182,7 @@ UdpTimedRx(struct ocx *ocx, const struct udp_socket *usc, ssize_t Udp_Send(struct ocx *ocx, const struct udp_socket *usc, - const void *ss, socklen_t sl, const void *buf, ssize_t len) + const void *ss, socklen_t sl, const void *buf, size_t len) { const struct sockaddr *sa; diff --git a/udp.h b/udp.h index 4b7ff9d..4c078c4 100644 --- a/udp.h +++ b/udp.h @@ -42,5 +42,5 @@ ssize_t UdpTimedRx(struct ocx *, const struct udp_socket *, void *, ssize_t len, double tmo); ssize_t Udp_Send(struct ocx *, const struct udp_socket *, - const void *sa, socklen_t, const void *ptr, ssize_t); + const void *sa, socklen_t, const void *ptr, size_t);