From 33876e122f6e7f51ea5d85b4b40e0d372bd76125 Mon Sep 17 00:00:00 2001 From: LI Qing Date: Wed, 4 Aug 2021 11:42:14 +0800 Subject: [PATCH] Fix the compiler's warnings in make test with glibc --- test/client/main.c | 13 +++++-------- test/device/main.c | 5 ++++- test/eventfd/Makefile | 2 +- test/exec/main.c | 3 ++- test/ioctl/main.c | 10 ++++++---- test/resolv_conf/main.c | 11 ++++++++--- test/server/main.c | 2 +- test/unix_socket/main.c | 12 +++++++----- 8 files changed, 34 insertions(+), 24 deletions(-) diff --git a/test/client/main.c b/test/client/main.c index 3d6a2e92..2eccbda5 100644 --- a/test/client/main.c +++ b/test/client/main.c @@ -1,3 +1,4 @@ +#define _GNU_SOURCE #include #include #include @@ -94,16 +95,11 @@ int client_sendmsg(int server_fd, char *buf) { #ifdef __GLIBC__ -struct mmsghdr { - struct msghdr msg; - unsigned int len; -}; - int client_sendmmsg(int server_fd, char *buf) { int ret = 0; struct mmsghdr msg_v[2] = {}; struct iovec iov[1]; - struct msghdr *msg_ptr = &msg_v[0].msg; + struct msghdr *msg_ptr = &msg_v[0].msg_hdr; // Set msg0 msg_ptr->msg_name = NULL; @@ -118,14 +114,15 @@ int client_sendmmsg(int server_fd, char *buf) { // Set msg1 msg_v[1] = msg_v[0]; - msg_ptr = &msg_v[1].msg; + msg_ptr = &msg_v[1].msg_hdr; msg_ptr->msg_iov = NULL; msg_ptr->msg_iovlen = 0; ret = sendmmsg(server_fd, msg_v, 2, 0); - if (ret != 2 || msg_v[0].len <= 0 || msg_v[1].len != 0) { + if (ret != 2 || msg_v[0].msg_len <= 0 || msg_v[1].msg_len != 0) { THROW_ERROR("sendmsg failed"); } + return 0; } #endif diff --git a/test/device/main.c b/test/device/main.c index 0dcd3f32..ce9ca1ed 100644 --- a/test/device/main.c +++ b/test/device/main.c @@ -1,3 +1,4 @@ +#define _GNU_SOURCE #include #include #include @@ -158,7 +159,9 @@ int test_dev_fd() { if (fd_str == NULL) { THROW_ERROR("calloc failed"); } - asprintf(&fd_str, "%d", fd); + if (asprintf(&fd_str, "%d", fd) < 0) { + THROW_ERROR("failed to asprintf"); + } strcat(dev_fd_path, fd_str); int dev_fd = open(dev_fd_path, O_WRONLY, 0666); if (dev_fd < 0) { diff --git a/test/eventfd/Makefile b/test/eventfd/Makefile index 3ec3f733..6f1fabb9 100644 --- a/test/eventfd/Makefile +++ b/test/eventfd/Makefile @@ -1,5 +1,5 @@ include ../test_common.mk -EXTRA_C_FLAGS := -Wno-incompatible-pointer-types-discards-qualifiers +EXTRA_C_FLAGS := -Wno-incompatible-pointer-types-discards-qualifiers -Wno-unused-result EXTRA_LINK_FLAGS := -lpthread BIN_ARGS := diff --git a/test/exec/main.c b/test/exec/main.c index ad6245c1..5bee1551 100644 --- a/test/exec/main.c +++ b/test/exec/main.c @@ -45,7 +45,8 @@ int test_execve_error_return(void) { } // during the time, try execve a non-exit process - int ret = execve("/bin/joke", NULL, NULL); + char *args[] = {"joke", NULL}; + int ret = execve("/bin/joke", args, NULL); if (ret != -1 || errno != ENOENT) { THROW_ERROR("execve error code wrong"); } diff --git a/test/ioctl/main.c b/test/ioctl/main.c index 0d1cb3cb..5ab75ea1 100644 --- a/test/ioctl/main.c +++ b/test/ioctl/main.c @@ -1,3 +1,4 @@ +#define _GNU_SOURCE #include #include #include @@ -5,7 +6,6 @@ #include #include #include -#define _GNU_SOURCE #include #include #include @@ -547,9 +547,11 @@ int test_ioctl_FIOCLEX(void) { child_argv[0] = strdup("naughty_child"); child_argv[1] = strdup("-t"); child_argv[2] = strdup("fioclex"); - asprintf(&child_argv[3], "%d", fd); - asprintf(&child_argv[4], "%d", pipefds[0]); - asprintf(&child_argv[5], "%d", pipefds[1]); + if (asprintf(&child_argv[3], "%d", fd) < 0 || + asprintf(&child_argv[4], "%d", pipefds[0]) < 0 || + asprintf(&child_argv[5], "%d", pipefds[1]) < 0) { + THROW_ERROR("failed to call asprintf"); + } ret = posix_spawn(&child_pid, "/bin/naughty_child", NULL, NULL, child_argv, NULL); if (ret != 0) { diff --git a/test/resolv_conf/main.c b/test/resolv_conf/main.c index d0fee16b..2b0678d7 100644 --- a/test/resolv_conf/main.c +++ b/test/resolv_conf/main.c @@ -12,10 +12,15 @@ char *read_resolv_conf(void) { fseek(fp, 0, SEEK_SET); char *resolv_conf_buffer = malloc(fsize + 1); if (resolv_conf_buffer == NULL) { - printf("ERROR: Failed to malloc for /etc/resolv.conf buffer"); + printf("ERROR: Failed to malloc for /etc/resolv.conf buffer\n"); + return NULL; + } + size_t len = fread(resolv_conf_buffer, 1, fsize, fp); + if (len != fsize) { + printf("ERROR: failed to fread correct len\n"); + fclose(fp); return NULL; } - fread(resolv_conf_buffer, 1, fsize, fp); fclose(fp); return resolv_conf_buffer; } @@ -45,4 +50,4 @@ static test_case_t test_cases[] = { int main() { return test_suite_run(test_cases, ARRAY_SIZE(test_cases)); -} \ No newline at end of file +} diff --git a/test/server/main.c b/test/server/main.c index 3176b23a..2be7ab9c 100644 --- a/test/server/main.c +++ b/test/server/main.c @@ -381,7 +381,7 @@ int test_poll() { char buf[512]; if ((count = read(client_fd, buf, sizeof buf)) != 0) { if (strcmp(buf, DEFAULT_MSG) != 0) { - printf(buf); + printf("%s", buf); THROW_ERROR("msg mismatched"); } } else { diff --git a/test/unix_socket/main.c b/test/unix_socket/main.c index 34b748b9..6bea81c1 100644 --- a/test/unix_socket/main.c +++ b/test/unix_socket/main.c @@ -160,8 +160,8 @@ int verify_child_echo(int *connected_sockets) { } char actual_str[32] = {0}; - read(connected_sockets[1], actual_str, 32); - if (strncmp(actual_str, ECHO_MSG, sizeof(ECHO_MSG) - 1) != 0) { + ssize_t len = read(connected_sockets[1], actual_str, 32); + if (len != sizeof(ECHO_MSG) || strncmp(actual_str, ECHO_MSG, strlen(ECHO_MSG)) != 0) { printf("data read is :%s\n", actual_str); THROW_ERROR("received string is not as expected"); } @@ -269,7 +269,9 @@ int test_poll() { THROW_ERROR("socketpair failed"); } - write(socks[0], "not today\n", 10); + if (write(socks[0], "not today\n", 10) < 0) { + THROW_ERROR("failed to write to socket"); + } struct pollfd polls[] = { { .fd = socks[0], .events = POLLOUT }, @@ -358,8 +360,8 @@ int test_ioctl_fionread() { } char actual_str[32] = {0}; - read(sockets[1], actual_str, 32); - if (strncmp(actual_str, ECHO_MSG, sizeof(ECHO_MSG) - 1) != 0) { + ssize_t len = read(sockets[1], actual_str, 32); + if (len != sizeof(ECHO_MSG) || strncmp(actual_str, ECHO_MSG, strlen(ECHO_MSG)) != 0) { printf("data read is :%s\n", actual_str); THROW_ERROR("received string is not as expected"); }