From 08318a0bacac0e54cada71f413dfe12da0efcb02 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 14 Aug 2020 14:19:44 +0200 Subject: [PATCH 1/2] n-dhcp4/packet: avoid compiler warning in n_dhcp4_c_socket_packet_recv() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit gcc-10.2.1-1.fc32 with optimizations and LTO enabled can think that "len" is uninitialized. Let packet_recv_udp() always set the length. ../src/n-dhcp4-socket.c: In function ‘n_dhcp4_c_socket_packet_recv.constprop’: ../src/n-dhcp4-incoming.c:210:29: error: ‘len’ may be used uninitialized in this function [-Werror=maybe-uninitialized] 210 | incoming->n_message = n_raw; | ^ ../src/n-dhcp4-socket.c:558:16: note: ‘len’ was declared here 558 | size_t len; | ^ https://github.com/nettools/n-dhcp4/commit/142eedcfc332e52f3d3fdfa557be4d77a934e62d --- shared/n-dhcp4/src/util/packet.c | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/shared/n-dhcp4/src/util/packet.c b/shared/n-dhcp4/src/util/packet.c index fb0313abe7..ef18b0b464 100644 --- a/shared/n-dhcp4/src/util/packet.c +++ b/shared/n-dhcp4/src/util/packet.c @@ -293,6 +293,8 @@ int packet_recvfrom_udp(int sockfd, ssize_t pktlen; size_t hdrlen; + *n_transmittedp = 0; + /* Peek packet to obtain the real IP header length */ pktlen = recv(sockfd, &ip_hdr.hdr, sizeof(ip_hdr.hdr), MSG_PEEK); if (pktlen < 0) @@ -304,7 +306,6 @@ int packet_recvfrom_udp(int sockfd, * discard it. */ recv(sockfd, NULL, 0, 0); - *n_transmittedp = 0; return 0; } @@ -313,7 +314,6 @@ int packet_recvfrom_udp(int sockfd, * This is not an IPv4 packet, discard it. */ recv(sockfd, NULL, 0, 0); - *n_transmittedp = 0; return 0; } @@ -324,7 +324,6 @@ int packet_recvfrom_udp(int sockfd, * header length, discard the packet. */ recv(sockfd, NULL, 0, 0); - *n_transmittedp = 0; return 0; } @@ -354,7 +353,6 @@ int packet_recvfrom_udp(int sockfd, * provided too small a buffer. In both cases, we simply drop * the packet. */ - *n_transmittedp = 0; return 0; } @@ -366,14 +364,12 @@ int packet_recvfrom_udp(int sockfd, * The packet is too small to even contain an entire UDP * header, so discard it entirely. */ - *n_transmittedp = 0; return 0; } else if ((size_t)pktlen < hdrlen + ntohs(udp_hdr.len)) { /* * The UDP header specified a longer length than the returned * packet, so discard it entirely. */ - *n_transmittedp = 0; return 0; } @@ -386,13 +382,10 @@ int packet_recvfrom_udp(int sockfd, /* IP */ if (ip_hdr.hdr.protocol != IPPROTO_UDP) { - *n_transmittedp = 0; return 0; /* not a UDP packet, discard it */ } else if (ip_hdr.hdr.frag_off & htons(IP_MF | IP_OFFMASK)) { - *n_transmittedp = 0; return 0; /* fragmented packet, discard it */ } else if (checksum && packet_internet_checksum(ip_hdr.data, hdrlen)) { - *n_transmittedp = 0; return 0; /* invalid checksum, discard it */ } @@ -411,7 +404,6 @@ int packet_recvfrom_udp(int sockfd, buf, pktlen, udp_hdr.check)) { - *n_transmittedp = 0; return 0; } } From 4e0e002092c763e989613018b1eb1ed9972849be Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 13 Aug 2020 21:37:31 +0200 Subject: [PATCH 2/2] n-dhcp4/connection: avoid compiler warning in n_dhcp4_c_connection_connect() about fd_udp uninitialized MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With LTO and optimizations enabled, we get a compiler warning about fd_udp not initialized: ../src/n-dhcp4-c-connection.c: In function ‘n_dhcp4_c_connection_connect’: ../src/n-dhcp4-c-connection.c:196:13: error: ‘fd_udp’ may be used uninitialized in this function [-Werror=maybe-uninitialized] 196 | r = epoll_ctl(connection->fd_epoll, | ^ ../src/n-dhcp4-c-connection.c:185:16: note: ‘fd_udp’ was declared here 185 | int r, fd_udp; | ^ https://github.com/nettools/n-dhcp4/commit/6c6e9368989eae699448611fc82f41d50bd39dd9 --- shared/n-dhcp4/src/n-dhcp4-c-connection.c | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/shared/n-dhcp4/src/n-dhcp4-c-connection.c b/shared/n-dhcp4/src/n-dhcp4-c-connection.c index 6becfb5087..3123525390 100644 --- a/shared/n-dhcp4/src/n-dhcp4-c-connection.c +++ b/shared/n-dhcp4/src/n-dhcp4-c-connection.c @@ -182,7 +182,8 @@ int n_dhcp4_c_connection_listen(NDhcp4CConnection *connection) { int n_dhcp4_c_connection_connect(NDhcp4CConnection *connection, const struct in_addr *client, const struct in_addr *server) { - int r, fd_udp; + _c_cleanup_(c_closep) int fd_udp = -1; + int r; c_assert(connection->state == N_DHCP4_C_CONNECTION_STATE_PACKET); @@ -200,27 +201,21 @@ int n_dhcp4_c_connection_connect(NDhcp4CConnection *connection, .events = EPOLLIN, .data = { .u32 = N_DHCP4_CLIENT_EPOLL_IO }, }); - if (r < 0) { - r = -errno; - goto exit_fd; - } + if (r < 0) + return -errno; r = packet_shutdown(connection->fd_packet); - if (r < 0) - goto exit_epoll; + if (r < 0) { + epoll_ctl(connection->fd_epoll, EPOLL_CTL_DEL, fd_udp, NULL); + return r; + } connection->state = N_DHCP4_C_CONNECTION_STATE_DRAINING; connection->fd_udp = fd_udp; + fd_udp = -1; connection->client_ip = client->s_addr; connection->server_ip = server->s_addr; - fd_udp = -1; return 0; - -exit_epoll: - epoll_ctl(connection->fd_epoll, EPOLL_CTL_DEL, fd_udp, NULL); -exit_fd: - close(fd_udp); - return r; } void n_dhcp4_c_connection_close(NDhcp4CConnection *connection) {