A bit of signed/unsigned cleanup work.

This commit is contained in:
Poul-Henning Kamp 2015-01-16 21:21:15 +00:00
parent 76ccc26b27
commit 449dc16d47
10 changed files with 49 additions and 46 deletions

View File

@ -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');

View File

@ -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);

2
ntp.h
View File

@ -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 *****************************************/

View File

@ -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;

View File

@ -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);

View File

@ -32,6 +32,7 @@
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#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]);

View File

@ -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, &param_tbl, list) {
if (strlen(pt->name) != l)

View File

@ -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);
}
/**********************************************************************

2
udp.c
View File

@ -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;

2
udp.h
View File

@ -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);