Skip to content

Commit 8d7d765

Browse files
authored
can-utils: fix sign-compare warnings (#513)
Fixing several build issues reported by Gary Bisson when he was building can-utils with clang (AOSP14). URL: #512 Reported-by: Gary Bisson (https://github.com/gibsson) Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
1 parent 30a46d7 commit 8d7d765

14 files changed

Lines changed: 46 additions & 44 deletions

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ PREFIX ?= /usr/local
4343

4444
MAKEFLAGS := -k
4545

46-
CFLAGS := -O2 -Wall -Wno-parentheses
46+
CFLAGS := -O2 -Wall -Wno-parentheses -Wsign-compare
4747

4848
HAVE_FORK := $(shell ./check_cc.sh "$(CC)" fork_test.c)
4949

candump.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -755,13 +755,13 @@ int main(int argc, char **argv)
755755
}
756756

757757
/* mark dual-use struct canfd_frame */
758-
if (nbytes < CANXL_HDR_SIZE + CANXL_MIN_DLEN) {
758+
if (nbytes < (int)CANXL_HDR_SIZE + CANXL_MIN_DLEN) {
759759
fprintf(stderr, "read: no CAN frame\n");
760760
return 1;
761761
}
762762

763763
if (cu.xl.flags & CANXL_XLF) {
764-
if (nbytes != CANXL_HDR_SIZE + cu.xl.len) {
764+
if (nbytes != (int)CANXL_HDR_SIZE + cu.xl.len) {
765765
printf("nbytes = %d\n", nbytes);
766766
fprintf(stderr, "read: no CAN XL frame\n");
767767
return 1;

canfdtest.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,12 @@ static int recv_frame(struct canfd_frame *frame, int *flags)
215215
ssize_t ret;
216216

217217
ret = recvmsg(sockfd, &msg, 0);
218-
if (ret != iov.iov_len) {
219-
if (ret < 0)
220-
perror("recvmsg() failed");
221-
else
222-
fprintf(stderr, "recvmsg() returned %zd", ret);
218+
if (ret < 0) {
219+
perror("recvmsg() failed");
220+
return -1;
221+
}
222+
if ((size_t)ret != iov.iov_len) {
223+
fprintf(stderr, "recvmsg() returned %zd", ret);
223224
return -1;
224225
}
225226

cangen.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -774,17 +774,17 @@ int main(int argc, char **argv)
774774
cu.fd.len = CANFD_MAX_DLEN;
775775
}
776776

777-
if (canxl && (ifr.ifr_mtu < CANXL_MIN_MTU)) {
777+
if (canxl && (ifr.ifr_mtu < (int)CANXL_MIN_MTU)) {
778778
printf("CAN interface not CAN XL capable - sorry.\n");
779779
return 1;
780780
}
781781

782-
if (canfd && (ifr.ifr_mtu < CANFD_MTU)) {
782+
if (canfd && (ifr.ifr_mtu < (int)CANFD_MTU)) {
783783
printf("CAN interface not CAN FD capable - sorry.\n");
784784
return 1;
785785
}
786786

787-
if (ifr.ifr_mtu == CANFD_MTU) {
787+
if (ifr.ifr_mtu == (int)CANFD_MTU) {
788788
/* interface is ok - try to switch the socket into CAN FD mode */
789789
if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_FD_FRAMES,
790790
&enable_canfx, sizeof(enable_canfx))){
@@ -793,7 +793,7 @@ int main(int argc, char **argv)
793793
}
794794
}
795795

796-
if (ifr.ifr_mtu >= CANXL_MIN_MTU) {
796+
if (ifr.ifr_mtu >= (int)CANXL_MIN_MTU) {
797797
/* interface is ok - try to switch the socket into CAN XL mode */
798798
if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_XL_FRAMES,
799799
&enable_canfx, sizeof(enable_canfx))){
@@ -1070,7 +1070,7 @@ int main(int argc, char **argv)
10701070
esi = i & 8;
10711071
}
10721072
/* generate CAN XL traffic if the interface is capable */
1073-
if (ifr.ifr_mtu >= CANXL_MIN_MTU)
1073+
if (ifr.ifr_mtu >= (int)CANXL_MIN_MTU)
10741074
canxl = ((i & 96) == 96);
10751075

10761076
rtr_frame = ((i & 24) == 24); /* reduce RTR frames to 1/4 */

canlogserver.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,13 +407,13 @@ int main(int argc, char **argv)
407407
return 1;
408408
}
409409

410-
if (nbytes < CANXL_HDR_SIZE + CANXL_MIN_DLEN) {
410+
if (nbytes < (int)CANXL_HDR_SIZE + CANXL_MIN_DLEN) {
411411
fprintf(stderr, "read: no CAN frame\n");
412412
return 1;
413413
}
414414

415415
if (cu.xl.flags & CANXL_XLF) {
416-
if (nbytes != CANXL_HDR_SIZE + cu.xl.len) {
416+
if (nbytes != (int)CANXL_HDR_SIZE + cu.xl.len) {
417417
printf("nbytes = %d\n", nbytes);
418418
fprintf(stderr, "read: no CAN XL frame\n");
419419
return 1;

cansend.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ int main(int argc, char **argv)
156156
}
157157
mtu = ifr.ifr_mtu;
158158

159-
if (mtu == CANFD_MTU) {
159+
if (mtu == (int)CANFD_MTU) {
160160
/* interface is ok - try to switch the socket into CAN FD mode */
161161
if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_FD_FRAMES,
162162
&enable_canfx, sizeof(enable_canfx))){
@@ -165,7 +165,7 @@ int main(int argc, char **argv)
165165
}
166166
}
167167

168-
if (mtu >= CANXL_MIN_MTU) {
168+
if (mtu >= (int)CANXL_MIN_MTU) {
169169
/* interface is ok - try to switch the socket into CAN XL mode */
170170
if (setsockopt(s, SOL_CAN_RAW, CAN_RAW_XL_FRAMES,
171171
&enable_canfx, sizeof(enable_canfx))){

isobusfs/isobusfs_cli.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,10 @@ static int isobusfs_cli_rx_one(struct isobusfs_priv *priv, int sock)
268268
return 0;
269269
}
270270

271-
static int isobusfs_cli_handle_events(struct isobusfs_priv *priv, int nfds)
271+
static int isobusfs_cli_handle_events(struct isobusfs_priv *priv, unsigned int nfds)
272272
{
273273
int ret;
274-
int n;
274+
unsigned int n;
275275

276276
for (n = 0; n < nfds && n < priv->cmn.epoll_events_size; ++n) {
277277
struct epoll_event *ev = &priv->cmn.epoll_events[n];
@@ -341,7 +341,7 @@ int isobusfs_cli_process_events_and_tasks(struct isobusfs_priv *priv)
341341
return ret;
342342

343343
if (nfds > 0) {
344-
ret = isobusfs_cli_handle_events(priv, nfds);
344+
ret = isobusfs_cli_handle_events(priv, (unsigned int)nfds);
345345
if (ret)
346346
return ret;
347347
}

isobusfs/isobusfs_cmn.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,7 @@ void isobusfs_cmn_dump_last_x_bytes(const uint8_t *buffer, size_t buffer_size,
823823
size_t start_offset = 0;
824824
char *output_ptr;
825825
unsigned char c;
826-
size_t remaining;
826+
int remaining;
827827
char output[80];
828828
int n, j;
829829

@@ -832,7 +832,7 @@ void isobusfs_cmn_dump_last_x_bytes(const uint8_t *buffer, size_t buffer_size,
832832

833833
for (size_t i = start_offset; i < buffer_size; i += 8) {
834834
output_ptr = output;
835-
remaining = sizeof(output);
835+
remaining = (int)sizeof(output);
836836

837837
n = snprintf(output_ptr, remaining, "%08lx: ",
838838
(unsigned long)(start_offset + i));

isobusfs/isobusfs_srv.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,10 @@ static int isobusfs_srv_recv_one(struct isobusfs_srv_priv *priv, int sock)
201201
return EXIT_SUCCESS;
202202
}
203203

204-
static int isobusfs_srv_handle_events(struct isobusfs_srv_priv *priv, int nfds)
204+
static int isobusfs_srv_handle_events(struct isobusfs_srv_priv *priv, unsigned int nfds)
205205
{
206206
int ret;
207-
int n;
207+
unsigned int n;
208208

209209
for (n = 0; n < nfds && n < priv->cmn.epoll_events_size; ++n) {
210210
struct epoll_event *ev = &priv->cmn.epoll_events[n];

isobusfs/isobusfs_srv_cm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ int isobusfs_srv_request_volume(struct isobusfs_srv_priv *priv,
3636
struct isobusfs_srv_client *client,
3737
struct isobusfs_srv_volume *volume)
3838
{
39-
int j;
39+
unsigned int j;
4040

4141
/* Check if the client already requested this volume */
4242
for (j = 0; j < ARRAY_SIZE(volume->clients); j++) {

0 commit comments

Comments
 (0)