Skip to content

Commit 208a528

Browse files
committed
tests: fix integer overflow in bandwidth-client rate computation
n_loop * nb_points * 1000 overflows int in TCP mode; compute rates in uint64_t and count the remainder byte per loop iteration
1 parent a6848d2 commit 208a528

1 file changed

Lines changed: 21 additions & 20 deletions

File tree

tests/bandwidth-client.c

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <unistd.h>
1111
#endif
1212
#include <errno.h>
13+
#include <inttypes.h>
1314
#include <stdlib.h>
1415
#include <string.h>
1516
#include <time.h>
@@ -46,7 +47,7 @@ int main(int argc, char *argv[])
4647
uint32_t start;
4748
uint32_t end;
4849
uint32_t bytes;
49-
uint32_t rate;
50+
uint64_t rate;
5051
int rc;
5152
int n_loop;
5253
int use_backend;
@@ -103,27 +104,27 @@ int main(int argc, char *argv[])
103104
end = gettime_ms();
104105
elapsed = end - start;
105106

106-
rate = (n_loop * nb_points) * G_MSEC_PER_SEC / (end - start);
107+
rate = (uint64_t) n_loop * nb_points * G_MSEC_PER_SEC / (end - start);
107108
printf("Transfer rate in points/seconds:\n");
108-
printf("* %d points/s\n", rate);
109+
printf("* %" PRIu64 " points/s\n", rate);
109110
printf("\n");
110111

111-
bytes = n_loop * (nb_points / 8) + ((nb_points % 8) ? 1 : 0);
112-
rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start);
112+
bytes = n_loop * ((nb_points / 8) + ((nb_points % 8) ? 1 : 0));
113+
rate = (uint64_t) bytes / 1024 * G_MSEC_PER_SEC / (end - start);
113114
printf("Values:\n");
114115
printf("* %d x %d values\n", n_loop, nb_points);
115116
printf("* %.3f ms for %d bytes\n", elapsed, bytes);
116-
printf("* %d KiB/s\n", rate);
117+
printf("* %" PRIu64 " KiB/s\n", rate);
117118
printf("\n");
118119

119120
/* TCP: Query and response header and values */
120121
bytes = 12 + 9 + (nb_points / 8) + ((nb_points % 8) ? 1 : 0);
121122
printf("Values and TCP Modbus overhead:\n");
122123
printf("* %d x %d bytes\n", n_loop, bytes);
123124
bytes = n_loop * bytes;
124-
rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start);
125+
rate = (uint64_t) bytes / 1024 * G_MSEC_PER_SEC / (end - start);
125126
printf("* %.3f ms for %d bytes\n", elapsed, bytes);
126-
printf("* %d KiB/s\n", rate);
127+
printf("* %" PRIu64 " KiB/s\n", rate);
127128
printf("\n\n");
128129

129130
printf("READ REGISTERS\n\n");
@@ -140,27 +141,27 @@ int main(int argc, char *argv[])
140141
end = gettime_ms();
141142
elapsed = end - start;
142143

143-
rate = (n_loop * nb_points) * G_MSEC_PER_SEC / (end - start);
144+
rate = (uint64_t) n_loop * nb_points * G_MSEC_PER_SEC / (end - start);
144145
printf("Transfer rate in points/seconds:\n");
145-
printf("* %d registers/s\n", rate);
146+
printf("* %" PRIu64 " registers/s\n", rate);
146147
printf("\n");
147148

148149
bytes = n_loop * nb_points * sizeof(uint16_t);
149-
rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start);
150+
rate = (uint64_t) bytes / 1024 * G_MSEC_PER_SEC / (end - start);
150151
printf("Values:\n");
151152
printf("* %d x %d values\n", n_loop, nb_points);
152153
printf("* %.3f ms for %d bytes\n", elapsed, bytes);
153-
printf("* %d KiB/s\n", rate);
154+
printf("* %" PRIu64 " KiB/s\n", rate);
154155
printf("\n");
155156

156157
/* TCP:Query and response header and values */
157158
bytes = 12 + 9 + (nb_points * sizeof(uint16_t));
158159
printf("Values and TCP Modbus overhead:\n");
159160
printf("* %d x %d bytes\n", n_loop, bytes);
160161
bytes = n_loop * bytes;
161-
rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start);
162+
rate = (uint64_t) bytes / 1024 * G_MSEC_PER_SEC / (end - start);
162163
printf("* %.3f ms for %d bytes\n", elapsed, bytes);
163-
printf("* %d KiB/s\n", rate);
164+
printf("* %" PRIu64 " KiB/s\n", rate);
164165
printf("\n\n");
165166

166167
printf("WRITE AND READ REGISTERS\n\n");
@@ -178,27 +179,27 @@ int main(int argc, char *argv[])
178179
end = gettime_ms();
179180
elapsed = end - start;
180181

181-
rate = (n_loop * nb_points) * G_MSEC_PER_SEC / (end - start);
182+
rate = (uint64_t) n_loop * nb_points * G_MSEC_PER_SEC / (end - start);
182183
printf("Transfer rate in points/seconds:\n");
183-
printf("* %d registers/s\n", rate);
184+
printf("* %" PRIu64 " registers/s\n", rate);
184185
printf("\n");
185186

186187
bytes = n_loop * nb_points * sizeof(uint16_t);
187-
rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start);
188+
rate = (uint64_t) bytes / 1024 * G_MSEC_PER_SEC / (end - start);
188189
printf("Values:\n");
189190
printf("* %d x %d values\n", n_loop, nb_points);
190191
printf("* %.3f ms for %d bytes\n", elapsed, bytes);
191-
printf("* %d KiB/s\n", rate);
192+
printf("* %" PRIu64 " KiB/s\n", rate);
192193
printf("\n");
193194

194195
/* TCP:Query and response header and values */
195196
bytes = 12 + 9 + (nb_points * sizeof(uint16_t));
196197
printf("Values and TCP Modbus overhead:\n");
197198
printf("* %d x %d bytes\n", n_loop, bytes);
198199
bytes = n_loop * bytes;
199-
rate = bytes / 1024 * G_MSEC_PER_SEC / (end - start);
200+
rate = (uint64_t) bytes / 1024 * G_MSEC_PER_SEC / (end - start);
200201
printf("* %.3f ms for %d bytes\n", elapsed, bytes);
201-
printf("* %d KiB/s\n", rate);
202+
printf("* %" PRIu64 " KiB/s\n", rate);
202203
printf("\n");
203204

204205
/* Free the memory */

0 commit comments

Comments
 (0)