Skip to content

Commit 449dc16

Browse files
committed
A bit of signed/unsigned cleanup work.
1 parent 76ccc26 commit 449dc16

File tree

10 files changed

+49
-46
lines changed

10 files changed

+49
-46
lines changed

main_sim_client.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ simfile_readline(struct ocx *ocx, struct todolist *tdl, void *priv)
121121

122122
while (1) {
123123
if (fgets(buf, sizeof buf, sf->input) == NULL) {
124-
Debug(ocx, "EOF on -s file (%s)", sf->filename);
124+
Debug(ocx, "EOF on -s file (%s)\n", sf->filename);
125125
exit(0);
126126
}
127127
p = strchr(buf, '\r');

ntimed.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ struct timestamp *TS_Nanosec(struct timestamp *storage,
132132
struct timestamp *TS_Double(struct timestamp *storage, double);
133133
double TS_Diff(const struct timestamp *t1, const struct timestamp *t2);
134134
int TS_SleepUntil(const struct timestamp *);
135-
void TS_Format(char *buf, ssize_t len, const struct timestamp *ts);
135+
void TS_Format(char *buf, size_t len, const struct timestamp *ts);
136136

137137
void TS_RunTest(struct ocx *ocx);
138138

ntp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ struct ntp_packet {
7878

7979
struct ntp_packet *NTP_Packet_Unpack(struct ntp_packet *dst, void *ptr,
8080
ssize_t len);
81-
ssize_t NTP_Packet_Pack(void *ptr, ssize_t len, struct ntp_packet *);
81+
size_t NTP_Packet_Pack(void *ptr, ssize_t len, struct ntp_packet *);
8282

8383
/* ntp_tools.c -- Handy tools *****************************************/
8484

ntp_packet.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ ts_2ntp32(uint8_t *dst, const struct timestamp *ts)
136136

137137
CHECK_OBJ_NOTNULL(ts, TIMESTAMP_MAGIC);
138138
assert(ts->sec < 65536);
139-
Be16enc(dst, ts->sec);
139+
Be16enc(dst, (uint16_t)ts->sec);
140140
Be16enc(dst + 2, ts->frac >> 48ULL);
141141
}
142142

@@ -149,7 +149,7 @@ ts_2ntp64(uint8_t *dst, const struct timestamp *ts)
149149
Be32enc(dst + 4, ts->frac >> 32ULL);
150150
}
151151

152-
ssize_t
152+
size_t
153153
NTP_Packet_Pack(void *ptr, ssize_t len, struct ntp_packet *np)
154154
{
155155
uint8_t *pbuf = ptr;
@@ -160,8 +160,10 @@ NTP_Packet_Pack(void *ptr, ssize_t len, struct ntp_packet *np)
160160
assert(np->ntp_version < 8);
161161
assert(np->ntp_stratum < 15);
162162

163-
pbuf[0] = (uint8_t)np->ntp_leap << 6;
164-
pbuf[0] |= np->ntp_version << 3;
163+
pbuf[0] = (uint8_t)np->ntp_leap;
164+
pbuf[0] <<= 3;
165+
pbuf[0] |= np->ntp_version;
166+
pbuf[0] <<= 3;
165167
pbuf[0] |= (uint8_t)np->ntp_mode;
166168
pbuf[1] = np->ntp_stratum;
167169
pbuf[2] = np->ntp_poll;

ntp_peer.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ NTP_Peer_Poll(struct ocx *ocx, const struct udp_socket *usc,
106106
const struct ntp_peer *np, double tmo)
107107
{
108108
char buf[100];
109-
ssize_t len;
109+
size_t len;
110110
struct sockaddr_storage rss;
111111
socklen_t rssl;
112112
ssize_t l;
@@ -121,7 +121,7 @@ NTP_Peer_Poll(struct ocx *ocx, const struct udp_socket *usc,
121121
len = NTP_Packet_Pack(buf, sizeof buf, np->tx_pkt);
122122

123123
l = Udp_Send(ocx, usc, np->sa, np->sa_len, buf, len);
124-
if (l != len) {
124+
if (l != (ssize_t)len) {
125125
Debug(ocx, "Tx peer %s %s got %zd (%s)\n",
126126
np->hostname, np->ip, l, strerror(errno));
127127
return (0);

ntp_tools.c

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <math.h>
3333
#include <stdio.h>
3434
#include <string.h>
35+
#include <stdarg.h>
3536

3637
#include "ntimed.h"
3738
#include "ntp.h"
@@ -70,6 +71,17 @@ NTP_Tool_Client_Req(struct ntp_packet *np)
7071
* XXX: Nanosecond precision is enough for everybody.
7172
*/
7273

74+
static void __printflike(3, 4)
75+
bxprintf(char **bp, const char *e, const char *fmt, ...)
76+
{
77+
va_list ap;
78+
79+
assert(*bp < e);
80+
va_start(ap, fmt);
81+
*bp += vsnprintf(*bp, (unsigned)(e - *bp), fmt, ap);
82+
va_end(ap);
83+
}
84+
7385
void
7486
NTP_Tool_Format(char *p, ssize_t len, const struct ntp_packet *pkt)
7587
{
@@ -82,58 +94,44 @@ NTP_Tool_Format(char *p, ssize_t len, const struct ntp_packet *pkt)
8294

8395
e = p + len;
8496

85-
p += snprintf(p, e - p, "[%d", pkt->ntp_leap);
86-
assert(p < e);
97+
bxprintf(&p, e, "[%d", pkt->ntp_leap);
98+
bxprintf(&p, e, " %u", pkt->ntp_version);
8799

88-
p += snprintf(p, e - p, " %u", pkt->ntp_version);
89-
assert(p < e);
90-
91-
p += snprintf(p, e - p, " %d", pkt->ntp_mode);
92-
assert(p < e);
100+
bxprintf(&p, e, " %d", pkt->ntp_mode);
93101

94-
p += snprintf(p, e - p, " %3u", pkt->ntp_stratum);
95-
assert(p < e);
102+
bxprintf(&p, e, " %3u", pkt->ntp_stratum);
96103

97-
p += snprintf(p, e - p, " %3u", pkt->ntp_poll);
98-
assert(p < e);
104+
bxprintf(&p, e, " %3u", pkt->ntp_poll);
99105

100-
p += snprintf(p, e - p, " %4d", pkt->ntp_precision);
101-
assert(p < e);
106+
bxprintf(&p, e, " %4d", pkt->ntp_precision);
102107

103108
TS_Format(buf, sizeof buf, &pkt->ntp_delay);
104-
p += snprintf(p, e - p, " %s", buf); assert(p < e);
105-
assert(p < e);
109+
bxprintf(&p, e, " %s", buf); assert(p < e);
106110

107111
TS_Format(buf, sizeof buf, &pkt->ntp_dispersion);
108-
p += snprintf(p, e - p, " %s", buf); assert(p < e);
109-
assert(p < e);
112+
bxprintf(&p, e, " %s", buf); assert(p < e);
110113

111-
p += snprintf(p, e - p, " 0x%02x%02x%02x%02x",
114+
bxprintf(&p, e, " 0x%02x%02x%02x%02x",
112115
pkt->ntp_refid[0], pkt->ntp_refid[1],
113116
pkt->ntp_refid[2], pkt->ntp_refid[3]);
114-
assert(p < e);
115117

116-
p += snprintf(p, e - p, " %.9f",
118+
bxprintf(&p, e, " %.9f",
117119
TS_Diff(&pkt->ntp_reference, &pkt->ntp_origin));
118-
assert(p < e);
119120

120121
TS_Format(buf, sizeof buf, &pkt->ntp_origin);
121-
p += snprintf(p, e - p, " %s", buf); assert(p < e);
122-
assert(p < e);
122+
bxprintf(&p, e, " %s", buf); assert(p < e);
123123

124-
p += snprintf(p, e - p, " %.9f",
124+
bxprintf(&p, e, " %.9f",
125125
TS_Diff(&pkt->ntp_receive, &pkt->ntp_origin));
126-
assert(p < e);
127126

128-
p += snprintf(p, e - p, " %.9f",
127+
bxprintf(&p, e, " %.9f",
129128
TS_Diff(&pkt->ntp_transmit, &pkt->ntp_receive));
130-
assert(p < e);
131129

132130
if (pkt->ts_rx.sec && pkt->ts_rx.frac) {
133-
p += snprintf(p, e - p, " %.9f]",
131+
bxprintf(&p, e, " %.9f]",
134132
TS_Diff(&pkt->ts_rx, &pkt->ntp_transmit));
135133
} else {
136-
p += snprintf(p, e - p, " %.9f]", 0.0);
134+
bxprintf(&p, e, " %.9f]", 0.0);
137135
}
138136
assert(p < e);
139137
}
@@ -173,10 +171,10 @@ NTP_Tool_Scan(struct ntp_packet *pkt, const char *buf)
173171

174172
INIT_OBJ(pkt, NTP_PACKET_MAGIC);
175173
pkt->ntp_leap = (enum ntp_leap)u_fields[0];
176-
pkt->ntp_version = u_fields[1];
174+
pkt->ntp_version = (uint8_t)u_fields[1];
177175
pkt->ntp_mode = (enum ntp_mode)u_fields[2];
178-
pkt->ntp_stratum = u_fields[3];
179-
pkt->ntp_poll = u_fields[4];
176+
pkt->ntp_stratum = (uint8_t)u_fields[3];
177+
pkt->ntp_poll = (uint8_t)u_fields[4];
180178
pkt->ntp_precision = (int8_t)floor(d_fields[0]);
181179
TS_Double(&pkt->ntp_delay, d_fields[1]);
182180
TS_Double(&pkt->ntp_dispersion, d_fields[2]);

param.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ Param_Tweak(struct ocx *ocx, const char *arg)
121121
Fail(ocx, 0, "Stopping after parameter query.\n");
122122
}
123123

124-
l = q - arg;
124+
assert (q >= arg);
125+
l = (unsigned)(q - arg);
125126

126127
TAILQ_FOREACH(pt, &param_tbl, list) {
127128
if (strlen(pt->name) != l)

time_stuff.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,11 @@ TS_SleepUntil(const struct timestamp *t)
140140
/**********************************************************************/
141141

142142
void
143-
TS_Format(char *buf, ssize_t len, const struct timestamp *ts)
143+
TS_Format(char *buf, size_t len, const struct timestamp *ts)
144144
{
145145
CHECK_OBJ_NOTNULL(ts, TIMESTAMP_MAGIC);
146146
uint64_t x, y;
147+
int i;
147148

148149
/* XXX: Nanosecond precision is enough for everybody. */
149150
x = ts->sec;
@@ -152,7 +153,8 @@ TS_Format(char *buf, ssize_t len, const struct timestamp *ts)
152153
y -= 1000000000ULL;
153154
x += 1;
154155
}
155-
assert(snprintf(buf, len, "%jd.%09jd", (intmax_t)x, (intmax_t)y) < len);
156+
i = snprintf(buf, len, "%jd.%09jd", (intmax_t)x, (intmax_t)y);
157+
assert(i < (int)len);
156158
}
157159

158160
/**********************************************************************

udp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ UdpTimedRx(struct ocx *ocx, const struct udp_socket *usc,
182182

183183
ssize_t
184184
Udp_Send(struct ocx *ocx, const struct udp_socket *usc,
185-
const void *ss, socklen_t sl, const void *buf, ssize_t len)
185+
const void *ss, socklen_t sl, const void *buf, size_t len)
186186
{
187187
const struct sockaddr *sa;
188188

udp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,5 @@ ssize_t UdpTimedRx(struct ocx *, const struct udp_socket *,
4242
void *, ssize_t len,
4343
double tmo);
4444
ssize_t Udp_Send(struct ocx *, const struct udp_socket *,
45-
const void *sa, socklen_t, const void *ptr, ssize_t);
45+
const void *sa, socklen_t, const void *ptr, size_t);
4646

0 commit comments

Comments
 (0)